Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# limited Dictionary

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?

like image 210
Haim Evgi Avatar asked Apr 28 '11 05:04

Haim Evgi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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#

like image 168
Joel Lee Avatar answered Sep 30 '22 01:09

Joel Lee