C# Programming Language: Dictionaries
Hi everyone, welcome back. In these examples, we will be going over Dictionaries in C#. Dictionaries can be used to store a collection of data, similar to that of a normal list. The main difference is that a Dictionary can store elements as key/value pairs as an alternative to using index numbers. The keys must be unique and cannot be null where the values can be duplicates or be null. With this introduction out of the way, let’s get into it.

Creating Dictionary
Let’s start by creating a Dictionary. Before we can create a Dictionary, we need to make sure that we have our Dictionary import, which is included within the System.Collections.Generic package:
using System.Collections.Generic;
Now, we can successfully create our Dictionary:
Dictionary<int, string> myDict = new Dictionary<int, string>();
So now, we have created our Dictionary variable. Our keys are of the ‘int’ data type and our values are of the ‘string’ data type. When we create our Dictionary, we will need to specify that data types for our keys and values. We can use other data types as well for both.
Adding Elements Into The Dictionary
Now, let’s see how we can add elements into the Dictionary. We can do this by using the built in Add() function that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
foreach(var element in myDict)
{
Console.WriteLine(element.Key + ": " + element.Value);
}Output:
1: One
4: Four
17: Seventeen
In the example above, we can see that we added a few elements into our Dictionary by using the Add() function. We can verify that they were successfully added by printing our Dictionary and checking the output.
Retrieving The Keys of a Dictionary
Now, let’s see how we can retrieve the keys of a Dictionary. We can do this by using the built in ‘Keys’ property that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
List<int> myKeys = new List<int>(myDict.Keys);
Console.WriteLine(String.Join(" ", myKeys));Output:
1 4 17
In the example above, we can see that we have successfully retrieved the list of keys within our Dictionary. We can verify that we retrieved the keys by printing the resulting list and checking the output.
Retrieving The Values of a Dictionary
Now, let’s see how we can retrieve the values of a Dictionary. We can do this by using the built in ‘Values’ property that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
List<string> myValues = new List<string>(myDict.Values);
Console.WriteLine(String.Join(" ", myValues));Output:
One Four Seventeen
In the example above, we can see that we have successfully retrieved the list of values within our Dictionary. We can verify that we retrieved the values by printing the resulting list and checking the output.
Checking The Size of The Dictionary
Now, let’s see how we can check the size of the Dictionary, or how many elements are in the Dictionary. We can do this by using the built in ‘Count’ property that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
Console.WriteLine(myDict.Count);Output:
3
In the example above, we can see that we added a few elements into our Dictionary. We then printed out the result of the count property on our Dictionary and we can check our output to see how many elements we have.
Checking if The Dictionary Contains a Specific Key
Now, let’s try to see if our Dictionary contains a specific key. We can do this by using the built in ContainsKey() function that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
Console.WriteLine(myDict.ContainsKey(1));
Console.WriteLine(myDict.ContainsKey(2));Output:
True
False
In the example above, we can see that we added a few elements into our Dictionary. Then we printed the results of our ContainsKey() functions. We checked to see if our Dictionary contains the keys of ‘1’ and ‘2.’ As we can see in the output, our Dictionary contains a key of ‘1,’ but not ‘2.’
Checking if The Dictionary Contains a Specific Value
Now, let’s try to see if our dictionary contains a specific value. We can do this by using the built in ContainsValue() function. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
Console.WriteLine(myDict.ContainsValue("One"));
Console.WriteLine(myDict.ContainsValue("Two"));Output:
True
False
In the example above, we can see that we added a few elements into our Dictionary. Then we printed the results of our ContainsValue() functions. We checked to see if our Dictionary contains the values of ‘One’ and ‘Two.’ As we can see in the output, our Dictionary contains a value of ‘One,’ but not ‘Two.’
Removing an Element From a Dictionary
Now, let’s see how we can remove an element from a Dictionary. We can do this by using the built in Remove() function that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
myDict.Remove(1);
foreach(var element in myDict)
{
Console.WriteLine(element.Key + ": " + element.Value);
}Output:
4: Four
17: Seventeen
In the example above, we can see that we added a few elements into our Dictionary. Then we used the Remove() function to remove the element with the key of ‘1’ from our Dictionary. We can verify that we successfully removed the corresponding element by printing our Dictionary and checking the output.
Clearing out a Dictionary
Now, let’s see how we can completely clear out a Dictionary. We can do this by using the built in Clear() function that is provided. Let’s see an example:
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "One");
myDict.Add(4, "Four");
myDict.Add(17, "Seventeen");
myDict.Clear();
foreach(var element in myDict)
{
Console.WriteLine(element.Key + ": " + element.Value);
}Output:
In the example above, we have added a few elements into our Dictionary. Then we used the Clear() function on our Dictionary. We can verify that all elements were removed by printing our Dictionary and checking the output. Our output should be empty because we removed everything.
Conclusion
That is it for the C# Dictionary examples. We covered how to create a Dictionary along with some of the functions regarding them. I hope this helps. If there are any questions or comments, please let me know. Thanks for reading.