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

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?

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.


List<> and Dictionary<,> - pretty different data structures which used for different purposes, List is simply a set of items and Dictionary is a set of key-value pairs.

Dictionary is pretty useful when you have a set of complex objects and want to have fast access by let's say ObjectName/ObjectId, in this case you create IDictionary<string, TObject> where key would be ObjectId and Value would be an object itself.

Some differences:

  • List persist order of the items, Dictionary does not
  • List allow fast access by index
  • List support built in QuickSort algorithm for fast data sorting
  • Dictionary allows ~O(1) time complexity to access an item (value) by a key

  • Dictionary<K,V> is an associative array, or map. It is a container that can be indexed by values of any type.
  • List<T> is an integer indexed array. It is a container that is indexed by contiguous integers.

The essential difference therefore is in how the containers are indexed.

Don't fall into the trap of believing that Dictionary<int,T> is semantically equivalent to List<T>. The difference is that the indexing of List<T> is contiguous whereas there can be gaps in the indexing for Dictionary<int,T>.


I have a class library which accesses a variety of T-sql sprocs; each sproc returns a single row but varying columns. I needed a general purpose solution for an retrieving the values and Dictionary<> provided a much cleaner solution than List<>.

The class common to all of the wrappers declares

public Dictionary<string, String> datadict = new Dictionary<string, string>();

and

public Dictionary<string, String> LoadData(string sproc, string paramName, string paramValue)

Invoking a Reader, the datadict is loaded with

for (int i = Reader.FieldCount; i != 0; i--)
 {
  datadict.Add(Reader.GetName(i - 1).Trim(), Reader.GetString(i - 1).Trim());
 }

and returns datadict to the calling class which can then retrieve the data much like Reader does; E.g.:

datadict = myData.LoadData("spGetSSN", "", "");
  ssn1 = datadict["SSN1"];
  ssn2 = datadict["SSN2"];
  ssn3 = datadict["SSN3"];

Much cleaner for me that List<>.


Need Your Help

How to get bounds of a google static map?

google-maps map latitude-longitude bounds

How to get bounds in degrees of google static map which has been returned, for example, for following request

TFS2010 BuildAgent using the .NET MSBuild instead of VS MSBuild

.net visual-studio msbuild visual-studio-2013

How can I configure the BuildAgent to build my projects using the new MSBuild, that has shipped with VisualStudio 2013, instead of the .NET framework MSBuild.