I want to build a Dictionary (key, value),
but I want that this Dictionary have limited size,
for example 1000 entries,
so when I rich this limit size, I want to remove the first element and add a new element(FIFO).
I want to use dictionary because I am always searching keys in the dictionary (i need that it will be fast)
How to do this?
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
OrderedDictionarydoes not use Generics, so there will be some inefficency due to boxing (items in the dictionary will be inserted asobject). The only way to overcome this is to create a dual data structure which has all items in the dictionary mirrored in aQueue(for FIFO), or aStack(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#