Use Dictionaries to Store Key/Value Pairs

Updated: Apr 17, 2020

When using an array or list object, you access the elements by using the integer index for those elements. The index automatically increments as more elements are added. Any key/value pair collection allows you to replace that auto-incrementing number with any datatype or value that you want to use to uniquely identify that element.

A library application would be an ideal place to use a dictionary object. Every book has a unique ISBN number that can be used to identify a book. Since ISBN are alpha-numeric, you would want to use a string data type for the “key”.

Dictionaries are initiated the same way as the List object with the new keyword.

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

Adding Elements

You can add elements to a dictionary during initialization using the curly braces { }. However, since there is no auto-incremented index, each key/value pair needs to be surrounded by their own braces.

 Dictionary<string, string> bookList = new Dictionary<string, string>()
      {"0199535566","Pride and Prejudice" },
      {"160252369X" ,"War and Peace"},
      {"0007661428","Green Ham and Eggs" }

It is import to remember that the keys must be unique. You cannot have to elements with the same key. If that were allowed, the application would not know which element to return. If you attempt to enter a duplicate key in a dictionary the application with crash with an “ArgumentException

You can also utilize the Add() method to create new elements in the dictionary.

 bookList.Add("1572706279", "The Great Gatsby");
 bookList.Add("0553296983", "Diary of Anne Frank");
 bookList.Add("0590846271", "Captain Underpants");

Accessing Elements

The key is used to a access a dictionary element. You can use it to change the value of the elements

 bookList["0007661428"] = "Green Eggs and Ham";
 bookList["0553296983"] = "Diary of Anne Frank";

Store the value in a variable

 string favoriteBook = bookList["0590846271"];

Or display the information to the user.

 Console.WriteLine("Your reading assignment is to finish " + bookList["160252369X"]);

To get the number of elements in a dictionary, use the Count property

 Console.WriteLine("I have " + bookList.Count + " books.");

Looping Through a Dictionary

You can use a foreach to loop through a dictionary, like you can a list. However, since every item in the dictionary has two datatypes, there is a slight difference.

Looping through a list of book titles, looks like this:

 List<string> bookTitles = new List<string>();
 foreach (string item in bookTitles)
      Console.WriteLine("The name of the book is: " + item);

To loop through a dictionary the data type of the loop is actually a KeyValuePair object with the same generic types as your dictionary:

 foreach (KeyValuePair<string,string> item in bookList)
      Console.WriteLine("The name of the book is: " + item.Value);
      Console.WriteLine("The ISBN is: " + item.Key);

Looping through Keys or Values

If you wanted to loop through just the keys or the values of the dictionary, the data type would not be KeyValuePair object. It would be the same data types as the key or the value, depending on what you want to loop through.

However, you will have to tell Visual Studio that you are actually looping through the keys or values and not the dictionary elements. You do this by adding “.Keys” or “.Values” to your foreach syntax.

 Console.WriteLine("Here is a list of all the book titles.");
 foreach (string item in bookList.Values)
      Console.WriteLine("Book Title: " + item);
 Console.WriteLine("Here is listing of ISBN numbers.");
 foreach (string item in bookList.Keys)
      Console.WriteLine("ISBN: " + item);

Removing Elements

To completely empty the dictionary of all elements, use the Clear() method.


To revemove specific elements from the dictionary, use the Remove() method along with the key of the element.



49 views0 comments

Recent Posts

See All