When to Use Dictionary Over List in C#

This article explains when to use a dictionary instead of a list.

The previous few articles of this series explained that the Dictionary class is a collection of key/value pairs, a List is a collection of strongly-typed objects. Also explained was how to sort simple and complex objects and how convert an Array to a generic.

We also discussed some important and useful methods of dictionaries and lists. But we haven't discussed when to use a dictionary instead of a list.

So, let's dive right in.

In Visual Studio create a new console application.

In the main project create two classes in the same cs file.

  1. class Abbreviation {  
  2. public string Code { getset; }  
  3. public string Name { getset; }  
  4. }  
  5.   
  6. class Program {  
  7. static void Main (string[] args) {  
  8.   
  9. }  

In the Main method of the Program class, create seven new objects of Abbreviation class.

  1. static void Main (string[] args) {  
  2. Abbreviation abbreviationOne = new Abbreviation() {  
  3. Code = "MON",  
  4. Name ="Monday"  
  5. };  
  6. Abbreviation abbreviationTwo = new Abbreviation() {  
  7. Code = "TUE",  
  8. Name ="Tuesday"  
  9. }; Abbreviation abbreviationThree = new Abbreviation() {  
  10. Code = "WED",  
  11. Name ="Wednesday"  
  12. }; Abbreviation abbreviationFour = new Abbreviation() {  
  13. Code = "THU",  
  14. Name ="Thursday"  
  15. }; Abbreviation abbreviationFive = new Abbreviation() {  
  16. Code = "FRI",  
  17. Name ="Friday"  
  18. }; Abbreviation abbreviationSix = new Abbreviation() {  
  19. Code = "SAT",  
  20. Name ="Saturday"  
  21. }; Abbreviation abbreviationSeven = new Abbreviation() {  
  22. Code = "SUN",  
  23. Name ="Sunday"  
  24. };  

Create a new List object of type Abbreviation and add all the seven objects to it.

  1. List<Abbreviation> AbbreviationList = new List<Abbreviation>();  
  2. AbbreviationList.Add(abbreviationOne);  
  3. AbbreviationList.Add(abbreviationTwo);  
  4. AbbreviationList.Add(abbreviationThree);  
  5. AbbreviationList.Add(abbreviationFour);  
  6. AbbreviationList.Add(abbreviationFive);  
  7. AbbreviationList.Add(abbreviationSix);  
  8. AbbreviationList.Add(abbreviationSeven);  

So, until now we have created a class “Abbreviation” with two auto-implemented properties. In the main method we created seven objects of the Abbreviation class and assigned their property values.

Then we created a list collection of abbreviations in which we added all the seven objects.

Now what we want to do is, we want to prompt the users to enter a code like mon or tue that will give the result as Monday or Tuesday. So, let's see how to do it.

Just after adding the seventh item in the AbbreviationList write the following code:

  1. Console.Write("Enter a code to get an abbreviation.");  
  2. Console.Write("For example Mon or mon which will give Monday");  
  3. Console.Write("----");  
  4. Console.WriteLine();  
  5. string EnterCode = Console.ReadLine().ToUpper(); 

So, if we run the application, the console window will look like this.



If the user types Mon then its abbreviation will be displayed.



But currently we haven't implemented this logic in our application. So, let's see how to do it.

In the list class there is the method “Find” that returns the first matching row.



This method expects a predicate. So, we can say:

AbbreviationList.Find(ab => ab.Code == EnterCode);

So, whatever the users will pass in, that EnterCode value will be passed in here as a find method parameter and this find method will look up for the abbreviation in the entire list of items.

Look at the return type of this find method, it returns an Abbreviation class object. That means we can store the data in a new object and from that object we can get the Name property that we can display on the console window.

  1. Abbreviation returnAbs = AbbreviationList.Find(ab => ab.Code == EnterCode);  
  2. Console.WriteLine  
  3.   
  4. ("The abbreviation for "+EnterCode +" is "+returnAbs.Name); 

Run the application.



Let's see how to do the same thing using a Dictionary.

Create a dictionary object and provide the key data type as string and the value type as Abbreviation. Use the add method to insert all the abbreviation objects.

In the first parameter pass the Code property of the abbreviation class and in the second parameter pass the abbreviation object itself.

  1. Dictionary<string, Abbreviation> DictionaryAbbreviation = new Dictionary<string, Abbreviation>();  
  2. DictionaryAbbreviation.Add(abbreviationOne.Code, abbreviationOne);  
  3. DictionaryAbbreviation.Add(abbreviationTwo.Code, abbreviationTwo);  
  4. DictionaryAbbreviation.Add(abbreviationThree.Code, abbreviationThree);  
  5. DictionaryAbbreviation.Add(abbreviationFour.Code, abbreviationFour);  
  6. DictionaryAbbreviation.Add(abbreviationFive.Code, abbreviationFive);  
  7. DictionaryAbbreviation.Add(abbreviationSix.Code, abbreviationSix);  
  8. DictionaryAbbreviation.Add(abbreviationSeven.Code, abbreviationSeven); 

To look for a key in a dictionary we can use the ContainsKey that is a Boolean method.

  1. Console.Write("Enter a code to get an abbreviation.");  
  2. Console.Write("For example Mon or mon which will give Monday");  
  3. Console.Write("----");  
  4. Console.WriteLine();  
  5. string EnterCode = Console.ReadLine().ToUpper();  
  6. if (DictionaryAbbreviation.ContainsKey(EnterCode)) { 

If the key is found then inside the if block pass the EnterCode as an index value to the DictionaryAbbreviation that will return an Abbreviation object back and from that object we can get the abbreviation for the code entered.

  1. if (DictionaryAbbreviation.ContainsKey(EnterCode)) {  
  2.   
  3. Abbreviation abss = DictionaryAbbreviation[EnterCode];  
  4.   
  5. Console.WriteLine("The abbreviation for "+EnterCode +" is "+abss.Name);  
  6.   
  7. }  
  8. else {  
  9. Console.WriteLine("Invalid key");  

Run the application.



Enter invalid key



Whenever we use List's Find method to look for an item it loops through each items present in the List until the match is found.



This Find method took 2023 ms to find a matching value.

The Dictionary class ContainsKey method just looks for the key.



This ContainsKey method took 3 ms to find a matching value.

So, if you want to just look up a value using a key then using Dictionary would be a better choice.

I hope you like it. Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all