Pages

Men

rh

7/01/2012

What’s the difference between Lists & Dictionaries in C#

What’s the difference between Lists & Dictionaries

Both lists and dictionaries are used to store collections of data. Assume we had the following declarations…
 
var dic = new Dictionary<string, long>();
var lst = new List<long>();
 
long data;

With a list, you simply add the item to the list and it will add the item to the end of the list.
lst.Add(data);

With a dictionary, you need to specify some sort of key and the data you want to add so that it can be uniquely identified.
dic.Add(uniquekey, data);

Because with a dictionary you now have unique identifier, in the background they provide all sort’s of optimized algorithms to find your associated data. What this means is that if you are wanting to access your data it is a lot faster than a List.

No comments :

Post a Comment