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;
Syntax
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");
Here, in the above code dic is an object of Dictionary. The key type is int and the value type is string. It defines the krishna value added to the Dictionary.
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");
Here, we add 4 values to the Dictionary and finally remove the key value "S" from the Dictionary.
Output
Count
This method is used to determine the total number of key/value pairs present in the Dictionary.
Syntax
Here, dic is the Dictionary parameter.
Output
Clear
If you want to remove all the pairs from the Dictionary we used this method.
Syntax
Retrive key/value pair from the Dictionary using a foreach loop
- foreach (KeyValuePair<string, string> item in dic)
- {
- Console.WriteLine("Key ->" + item.Key.ToString() + "Value ->" + item.Value.ToString());
- }
Here, KeyValuePair stores two values together, it is simple and available in the Dictionary. You can use var also replacing KeyValuePair.
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");
- }
This is a simple example to search for a key or value (if present) in a Dictionary.
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)
- {
-
- var dic = new Dictionary<string, string>();
-
-
- dic.Add("K","Krishna");
- dic.Add("R", "Radha");
- dic.Add("J", "Jeetendra");
- dic.Add("S", "Sai");
-
- Console.WriteLine("After Adding the record in Dictionary");
-
- foreach (var pair in dic)
- {
- Console.WriteLine("Key ->" + pair.Key);
- Console.WriteLine("Value ->" + pair.Value);
- }
-
-
- var cnt = dic.Count;
- Console.WriteLine("Total Number of Pairs is " + cnt);
-
-
- 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("");
-
-
- 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("");
-
-
- if (dic.ContainsKey("K"))
- {
- Console.WriteLine("Key is Present");
- }
- else
- {
- Console.WriteLine("Key is not Present");
- }
-
- Console.WriteLine("");
-
-
- if (dic.ContainsValue("Jeetendra"))
- {
- Console.WriteLine("Value is Present");
- }
- else
- {
- Console.WriteLine("Value is not Present");
- }
-
-
- dic.Clear();
-
- Console.WriteLine("Finish");
- Console.ReadKey();
- }
- }
- }
Note: For detailed code please download the
Zip file attached above.
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.