To get both a dictionary and either LIFO/FIFO behavior (for deleting newest/oldest entry), you can use an OrderedDictionary
. See http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx.
To make this convenient to use, you could derive your own class from OrderedDictionary
, along the lines suggested by @ArsenMkrt.
Note, however, that OrderedDictionary
does not use Generics, so there will be some inefficency due to boxing (items in the dictionary will be inserted as object
). The only way to overcome this is to create a dual data structure which has all items in the dictionary mirrored in a Queue
(for FIFO), or a Stack
(for LIFO). For details see the answer by "Qua" to the following SO question, which deals with precisely the situation where you need an efficient way keep track of the order in which dictionary items were inserted.
Fastest and most efficient collection type in C#