Everything You Need to Know About the .NET Dictionary Collection Type

The Dictionary collection, just like all the other specialized .NET collection types, has a wide range of use cases that it is perfectly suited to. This can make it difficult to pick between the different types because in many cases, one of several will do just fine. After this post, you should be able to recognize when the Dictionary type is a potential solution and how to implement and use it.

What is a Dictionary?

The dictionary collection type is similar to a real-world dictionary. In a physical dictionary, it is typically organized by word or term and each word or term has an underlying definition. It isn’t just a definition, though. It will typically have usage examples, a pronunciation guide, synonyms, antonyms, and so on. The dictionary collection type also has a term, called a key, and that key supports a backing value that could be a string, integer, object, class, or any other type. The key itself can be any type including a GUID, string, number, and so on.

How Do We Use a Dictionary?

In order to get to the object stored in the Dictionary, you need the key for that object. Assuming that our dictionary has a key of “test”, we can pull out the value stored in the dictionary for “test” simply.

Figure 1.1 – Accessing Items of the Dictionary by Key

Dictionary<string, string> dictValues = new Dictionary<string, string>();

dictValues.Add("test", "test-value");
dictValues.Add("test2", "test2-value");

Console.WriteLine(dictValues["test"]);

As you can see in the code above, you have to declare the types of both the key and the value when the dictionary is created. The first string is the key and the second is the value being stored. Again, neither of these types need to be a string. They can be almost anything.

Figure 1.2 – Output from Listing 1.1

test-value

Sometimes you will know what the keys in your dictionary are and sometimes, you won’t. Strictly speaking, there are valid scenarios from a design perspective where you will not know or have access to the keys. One common situation would be where you are storing objects by key and need to keep track of them but you don’t know at design time what those object values will be such as a caching mechanism. In this type of application, if your dictionary has the object, you can retrieve it. If it doesn’t, you will need to look it up and then add it to the dictionary. Getting a list of the keys is also fairly straightforward.

Figure 1.3 – Retrieving a List of Dictionary Keys

var keys = dictValues.Keys;

foreach (var key in keys)
{
    Console.WriteLine(key);
}

This code will get the keys for the dictionary, loop through those keys, and print them out to the Console.

Figure 1.4 – Output from Figure 1.3

test
test2

Finally, you will most likely need to test whether or not the dictionary has a specific key added or not and potentially a stored value or not.

Figure 1.5 – ContainsKey and ContainsValue Usage

if (dictValues.ContainsKey("test"))
{
    Console.WriteLine(dictValues["test"]);
}

if(dictValues.ContainsValue("test2-value"))
{
    Console.WriteLine("Dictionary contains test2-value");
}

if(dictValues.ContainsValue("test3-value"))
{
    Console.WriteLine("test3-value");
}

In the code above, the first two tests should pass but the third should fail.

Figure 1.6 – Output from Figure 1.5

test-value
Dictionary contains test2-value

Conclusion

Though lesser known and rarely used, the Dictionary collection is a powerful addition to your toolbelt. After you discover the dictionary type, suddenly you will spot valid scenarios for its use all around you.

Leave a Reply

Your email address will not be published.