Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between list<> and dictionary<> in c#

Tags:

I have a strange doubt regarding list and dictionary in c#

In a list we add items to list by using the following method

using System.Collections.Generic;  class Program {     static void Main()     {        List<int> list = new List<int>();        list.Add(2);        list.Add(3);        list.Add(5);        list.Add(7);     } } 

In a dictionary we add items like this ...

using System; using System.Collections.Generic;  class Program {    static void Main()    {       Dictionary<string, int> d = new Dictionary<string, int>();       d.Add("cat", 2);       d.Add("dog", 1);       d.Add("llama", 0);       d.Add("iguana", -1);    } } 

I don't know exactly what is the difference, but in a dictionary we add items like a (key,value) pair and in a list we just add items without specifying any key ..

Would anyone clarify this?

like image 967
rockyashkumar Avatar asked Oct 27 '11 10:10

rockyashkumar


People also ask

What is the difference between list and dictionary?

A list is an ordered sequence of objects, whereas dictionaries are unordered sets. However, the main difference is that items in dictionaries are accessed via keys and not via their position.

Which is better list or dictionary in C#?

The larger the list, the longer it takes. Of course the Dictionary in principle has a faster lookup with O(1) while the lookup performance of a List is an O(n) operation. The Dictionary map a key to a value and cannot have duplicate keys, whereas a list just contains a collection of values.

Which is faster dictionary or list?

A dictionary is 6.6 times faster than a list when we lookup in 100 items.

How are dictionaries different from lists explain with examples?

List is a collection of index values pairs as that of array in c++. Dictionary is a hashed structure of key and value pairs. The indices of list are integers starting from 0. The keys of dictionary can be of any data type.


1 Answers

IDictionary is for key->value maps, ICollection is for sets of similar objects.

ICollection is an interface for collections of similar objects: the controls on a form, the elements in a list, the attributes in an XML tag, and so on. As of .NET 2.0, there's a generic version, so you can refer to a collection of integers as ICollection<int>.

IDictionary is an interface for mapping one type of object or value to another. It works like a real dictionary, or a phone book: you have a "key" in mind like a person’s name, and when you look it up, you get some information that’s identified by that key, like an address or phone number. Each key can only be listed once, although two different keys are still allowed to have the same value. This is also generic in .NET 2.0, so a dictionary whose keys are strings and whose values are integers would be IDictionary<string,int>.

A dictionary is actually a collection of key/value pairs: you can use an IDictionary<int,string> as an ICollection<KeyValuePair<int,string>>, and you can access the keys and values as separate collections with the Keys and Values properties.

Both ICollection and IDictionary are unordered, meaning that although you can retrieve the elements in some order with the CopyTo method or a foreach loop, that order has no special meaning, and it might change for no apparent reason. That’s the main difference between ICollection and IList: a list lets you put items in specific positions, just like an array, and they stay there until you move them.

like image 81
SShebly Avatar answered Oct 05 '22 19:10

SShebly