What is the difference between list and dictionary in C#?

CsharpProgrammingServer Side Programming

Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collection.Generics namespace. Dictionary is a generic type and returns an error if you try to find a key which is not there.

List collection is a generic class and can store any data types to create a list.

A list is a group of items −

List<string> myList = new List<string>() {
   "Maths",
   "English",
"   Science"
};

Dictionary is a set of key-value pairs.

Dictionary<string, int> d = new Dictionary<string, int>();

d.Add("squash", 1);
d.Add("football", 2);
d.Add("rugby", 3);

Looping is easier and faster in a list and access element using index easily with a List.

raja
Published on 20-Aug-2018 09:09:41
Advertisements