C# - SPLessons

How to use C# Dictionary Class

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How to use C# Dictionary Class

How to use C# Dictionary Class

Description :
Hi Friends, let me give you a intro about "Dictionary". It is a collection of keys and values pair of data. It has two parameters which are  : TKey - Define the type of the keys in the dictionary. TValue - Define the type of the values in the dictionary. Now we will see How to use C# Dictionary Class.

Namespace :
[csharp] using System.Collections.Generic; [/csharp]

Step1 :
Please open visual studio, right now i have visual studio 2013 i just opened my visual studio and click on new project.

[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons1.jpg[/image]

Step2 :
In the new project window please click on templates visual c# and then click on windows  and on the right side panel just click on console application. Please give a name for the console application and click ok button.

[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons2.jpg[/image]

Step3 :
If you see the visual studio will gives you a predefined template program  for us.

[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons3.jpg[/image]

Step4 :
Lets, Build simple console application to Insert, Delete, Search with key, Get all keys and  Display items from Dictionary.

Main Method :
[csharp] public static Dictionary<string, string> dictionarytable = new Dictionary<string, string>(); public static Boolean flag = true; static void Main(string[] args) { while (flag) { Console.WriteLine("\n1. Insert \t\t2. Remove \n3. Search with Key \t4. Get only Keys\n5. Display\t\t6. Exit"); Console.Write("Please enter your Choice: "); string choice = Console.ReadLine(); switch (choice) { case "1": dictionarytable = Insert(dictionarytable); break; case "2": dictionarytable = Remove(dictionarytable); break; case "3": FindValueUsingKey(dictionarytable); break; case "4": GetKeysInList(dictionarytable); break; case "5": Display(dictionarytable); break; case "6": flag = false; break; default: Console.WriteLine("Enter correct choice !!"); break; } } } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons4.jpg[/image]

Insert :
[csharp] private static Dictionary<string, string> Insert(Dictionary<string, string> dictionarytable) { Console.Write("Please enter data into Dictionary Table.\nKey : "); string key = Console.ReadLine(); Console.Write("Value : "); string value = Console.ReadLine(); dictionarytable.Add(key, value); Display(dictionarytable); return dictionarytable; } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons5.jpg[/image]

Delete :
[csharp] private static Dictionary<string, string> Remove(Dictionary<string, string> dictionarytable) { Console.Write("Please enter key of value to remove it from Dictionary Table.\nKey : "); string key = Console.ReadLine(); dictionarytable.Remove(key); Display(dictionarytable); return dictionarytable; } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons6.jpg[/image]

Search with Key :
[csharp] private static void FindValueUsingKey(Dictionary<string, string> dictionarytable) { Console.Write("Please enter key of value in the Dictionary Table.\nKey : "); string key = Console.ReadLine(); bool status = dictionarytable.ContainsKey(key); if(status) { Console.WriteLine(key + " with " + dictionarytable[key] + " is there in Dictionary Table."); } else { Console.WriteLine(key + " don't have any value."); } } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons7.jpg[/image]

Get all Keys :
[csharp] private static void GetKeysInList(Dictionary<string, string> dictionarytable) { Console.WriteLine("Keys in Dictionary Table :"); Dictionary<String, String>.KeyCollection keys = dictionarytable.Keys; foreach(string item in keys) { Console.WriteLine(item); } } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons8.jpg[/image]

Display :
[csharp] private static void Display(Dictionary<string, string> dictionarytable) { Console.WriteLine("Item in Dictionary Table :"); foreach(KeyValuePair<string,string> item in dictionarytable) { Console.WriteLine(item.Key + "\t: " + item.Value); } } [/csharp]

Output :
[image url=””]/wp-content/uploads/2014/09/Dictionary-csharp-Splessons9.jpg[/image]

Other Methods :
There are many other methods which you can try.

Count :
Gets the number of elements contained in the Dictionary.

ContainsValue :
Determines whether the Dictionary  contains a specific value.

ValueCollection :
Represents the collection of values in a Dictionary.

TryGetValue :
Gets the value associated with the specified key.

Linq Operation :
We can perform lambda expressions like Join, etc. on Dictionary etc.

Enjoy to Code..;)