SiteMap  | About    

C# Dictionary

How to C# Dictionary

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.

using System.Collections.Generic


c# dictionary sample

Parameters :

TKey - The type of the keys in the dictionary. TValue - The type of the values in the dictionary.


Dictionary<string, string> Dictionary<string, int>

Adding Values to Dictionary

Add method in Dictionary takes two parameters, one for the key and one for the value.


public void Add(TKey key,TValue value)



Key in a Dictionary should not be null, but a value can be, if TValue is a reference type.

Dictionary<string, int> dict = new Dictionary<string, int>(); dict.Add("one", 1); dict.Add("two", 2); dict.Add("three", 3); dict.Add("four", 4);

Retrieve Key-Value pair from Dictionary

We can retrieve values from Dictionary using foreach loop

foreach (KeyValuePair<string, int> pair in dict) { MessageBox.Show(pair.Key.ToString ()+ " - " + pair.Value.ToString () ); }

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.

if (dict.ContainsKey("four") == true) { MessageBox.Show(dict["four"].ToString ()); } else { MessageBox.Show("Key does not exist"); }

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

C# Dictionary and 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

C# Dictionary and 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

Dictionary < string, int > dict = new Dictionary < string, int > ();
dict.Add("one", 1);
dict.Add("two", 2);
dict.Add("three", 3);
dict.Add("four", 4);

foreach (KeyValuePair < string, int >  pair in dict)
	MessageBox.Show(pair.Key.ToString() + "  -  " + pair.Value.ToString());
} (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.