How to C# Dictionary
A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.
This class is defined in the System.Collections.Generic namespace, so you should import or using System.Collections.Generic namespace.
Adding Values to Dictionary
Add method in Dictionary takes two parameters, one for the key and one for the value.
Key in a Dictionary should not be null, but a value can be, if TValue is a reference type.
Retrieve Key-Value pair from Dictionary
We can retrieve values from Dictionary using foreach loop
Search for a Key
We can search a Key in Dictionary by using the ContainsKey method to test whether a key exists or not. ContainsKey computes the hashcode for its argument and checks the internal structures in the Dictionary.
Iterate over a Dictionary
There are many different ways to iterate over a Dictionary in C#. From the following link you can see in detail .... How to Iterate over a Dictionary
Dictionary Versus HashTable
Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out. Since both Dictionary and Hashtable are internally hashtables, so fast access to many-item data according to key, also both need immutable and unique keys. More about.... Dictionary Vs HashTable
Dictionary Versus List
Both lists and dictionaries are used to store collections of data. The Dictionary is based on a hash table, that means it uses a hash lookup, which is a rather efficient algorithm to look up things, on the other hand, a list you have to go element by element until it finds the result from beginning to the result each time. More about.... Dictionary vs List
Following is a full source code of adding and retrieving key values pairs in Dictionary