C # limited Dictionary

I want to build a dictionary (key, value), but I want this dictionary to have a limited size, for example 1000 entries, so when I am rich with this limit size, I want to delete the first element and add a new element (FIFO).

I want to use a dictionary because I am always looking for keys in a dictionary (I need it to be fast)

How to do it?

+5
source share
3 answers

To get both the dictionary and the LIFO / FIFO behavior (to delete the newest / oldest record), you can use OrderedDictionary. See http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx .

, OrderedDictionary , @ArsenMkrt.

, , OrderedDictionary Generics, - ( object). - , Queue ( FIFO) Stack ( LIFO). . "Qua" SO, , , .

#

+6

ovverride ,

if(myDic.Count == MAXCOUNT - 1)
{
    myDic.Remove(myDic[0]);
}
myDic.Add(key, item);
+3

, . .

I solved the problem by typing one method that checks the number of dictionaries manually, and when its size exceeds a certain limit, it will delete some entries from the dictionary.

0
source

All Articles