using System.Collections.Generic;
public static void Main()
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.TryGetValue("satoshi", out var result);
Console.WriteLine(result);
ICache<Person, int> cache = new LRUCache<Person, int>(5);
Person p = new Person("satoshi");
Console.WriteLine(cache.Get(p));
Console.WriteLine(cache.Get(p));
public Person(string name)
public interface ICache<K, V>
public class LRUCache<K, V> : ICache<K, V>
public K Key { get; set; }
public V Value { get; set; }
private readonly int _cap;
private readonly Dictionary<K, LinkedListNode<Item>> _cache;
private readonly LinkedList<Item> _lruList;
public LRUCache(int capacity)
this._cache = new Dictionary<K, LinkedListNode<Item>>(capacity);
this._lruList = new LinkedList<Item>();
if (this._cache.TryGetValue(key, out var node))
var value = node.Value.Value;
this._lruList.Remove(node);
this._lruList.AddLast(node);
public void Put(K key, V val)
throw new ArgumentNullException(nameof(key), "It cannot be null!");
if (this._cache.Count >= this._cap)
var lastNode = this._lruList.First;
this._cache.Remove(lastNode.Value.Key);
this._lruList.RemoveFirst();
var newItem = new LinkedListNode<Item>(new Item { Key = key, Value = val });
this._lruList.AddLast(newItem);
this._cache[key] = newItem;
return this._cache.Count;