Introduction
In this article you learn about the Dictionary collection, including how to use it and what the operations are that can be done on a Dictionary collection.
Dictionary
It is a collection of key and value pairs of data. It uses keys and values of a specified type including int and string. The key in the Dictionary should not be null but the value can be null. This class is defined in the following namespace that you can just import to use a Dictionary:
- using System.Collections.Generic;
Dictionary<TKey, TValue>
Parameters
TKey: The type of key used in the Dictionary.
TValue: The type of value used in the Dictionary.
Adding Values
The Add method is used to add values to the Dictionary, it takes two parameters; one for the key and the other for the value.
Syntax
- var dic = new Dictionary<int, string>();
- dic.Add(1,"Krishna");
Output

Removing Values
You can remove any key/value pairs from the Dictionary. Just pass the key value to the remove method.
Syntax
- var dic = new Dictionary<string, string>();
- dic.Add("K","Krishna");
- dic.Add("R", "Radha");
- dic.Add("J", "Jeetendra");
- dic.Add("S", "Sai");
- dic.Remove("S");
Output

Count
This method is used to determine the total number of key/value pairs present in the Dictionary.
Syntax
- dic.Count();
Output

Clear
If you want to remove all the pairs from the Dictionary we used this method.
Syntax
- dic.Clear();
- foreach (KeyValuePair<string, string> item in dic)
- {
- Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());
- }
Output

ContainsValue and ContainsKey
In the Dictionary class these two methods are used to search for a key or value in the Dictionary in a simple way.
- if (dic.ContainsKey("K"))
- {
- Console.WriteLine("Key is Present");
- }
- if (dic.ContainsValue("Jeetendra"))
- {
- Console.WriteLine("Value is Present");
- }
Output

Demo Example
In this example you learn in detail about Dictionary.
- Program.cs
- using System;
- using System.Collections.Generic;
- namespace Dictionary_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Creating Dictionary
- var dic = new Dictionary<string, string>();
- //Adding values to the Dictionary
- dic.Add("K","Krishna");
- dic.Add("R", "Radha");
- dic.Add("J", "Jeetendra");
- dic.Add("S", "Sai");
- Console.WriteLine("After Adding the record in Dictionary");
- //Retrive values using simple way
- foreach (var pair in dic)
- {
- Console.WriteLine("Key ->" + pair.Key);
- Console.WriteLine("Value ->" + pair.Value);
- }
- //Total number of pairs present in Dictionary
- var cnt = dic.Count;
- Console.WriteLine("Total Number of Pairs is " + cnt);
- //Removing values from Dictionary
- dic.Remove("S");
- Console.WriteLine("After Removing key in Dictionary");
- foreach (var pair in dic)
- {
- Console.WriteLine("Key ->" + pair.Key);
- Console.WriteLine("Value ->" + pair.Value);
- }
- Console.WriteLine("");
- //Retrive values using KeyValuePair
- Console.WriteLine("Retrive values using KeyValuePair");
- foreach (KeyValuePair<string, string> item in dic)
- {
- Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());
- }
- Console.WriteLine("");
- //Searching Key in Dictionary
- if (dic.ContainsKey("K"))
- {
- Console.WriteLine("Key is Present");
- }
- else
- {
- Console.WriteLine("Key is not Present");
- }
- Console.WriteLine("");
- //Searching Value in Dictionary
- if (dic.ContainsValue("Jeetendra"))
- {
- Console.WriteLine("Value is Present");
- }
- else
- {
- Console.WriteLine("Value is not Present");
- }
- //Remove all pairs from Dictionary
- dic.Clear();
- Console.WriteLine("Finish");
- Console.ReadKey();
- }
- }
- }
Summary
I hope you now understand the creation and use of a Dictionary. If you have any suggestion regarding this article then please contact me.

Sandeep VemulaPosted Oct 9, 2014, 1:24 AM
good n nice explain
Jeetendra GundPosted Oct 6, 2014, 12:59 PM
sure..Thanks Vithal Wadje sir
Vithal WadjePosted Oct 2, 2014, 2:01 PM
good one,keep writing on future as wel