When I started learning about Data Structures in programming, dictionaries, in particular, intrigued me a lot. And that is exactly why we will explore one of the most powerful data structures in C#: the dictionary. Whether looking to store and access key-value pairs or perform fast lookups and retrievals, dictionaries are essential in any C# programmer’s arsenal.
What is a Dictionary in C#?
At its core, a dictionary is a collection of key-value pairs, where each key is associated with a unique value. Dictionaries allow you to quickly and easily access and retrieve data based on its corresponding key.
How to Create a Dictionary in C#?
Let’s start by creating a dictionary in C#. Here’s the basic syntax for creating a new dictionary:
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
In this example, we’re creating a new dictionary that will store strings as keys and integers as values. Of course, you can customize this to fit your specific use case.
Adding Key-Value Pairs to a Dictionary
Now that we’ve created a dictionary let’s add key-value pairs. There are two ways to add key-value pairs to a dictionary:
Using the Add() Method
Here’s how to add a new key-value pair to a dictionary using the Add() method:
In this example, we’re adding a new key-value pair to our dictionary: the key “apple” is associated with the value 5.
Using the Indexer
Alternatively, you can add a new key-value pair to a dictionary using the indexer:
myDictionary["banana"] = 2;
In this example, we add a new key-value pair to our dictionary: the key “banana” is associated with the value 2.
How to Retrieve Values from a Dictionary in C#
Of course, adding key-value pairs to a dictionary is just the beginning. The real power of dictionaries comes from their ability to quickly and efficiently retrieve values based on their corresponding keys. Here’s how to retrieve a value from a dictionary:
int value = myDictionary["apple"];
In this example, we’re retrieving the value associated with the key “apple” from our dictionary. To get output, follow all the above lines that are mentioned, and then add the following line at the end:
At this point, the terminal will display the value as “5,” meaning you have successfully retrieved the value from the dictionary.
Checking for Key Existence
Before retrieving a value from a dictionary, you should always check whether the key exists. You can do this using the ContainsKey() method:
int value = myDictionary["apple"];
// do something with the value
In this example, we check whether the key “apple” exists in our dictionary before retrieving its corresponding value. If you print out the value of the “value” variable within the if-clause, then the terminal will show the output as “5”
Dictionaries have several useful properties that you can use to access information about them:
The Count property returns the number of key-value pairs in the dictionary:
int count = myDictionary.Count;
The Keys property returns a collection of all the keys in the dictionary:
foreach (string key in myDictionary.Keys)
The Values property returns a collection of all the values in the dictionary:
foreach (int value in myDictionary.Values)
And there you have it. With these simple steps and examples, you can confidently create and use dictionaries in your C# code. Whether building a complex application or experimenting with a new data structure, dictionaries offer a powerful and efficient way to store and retrieve key-value pairs. Read more C# & .NET Tutorials!