C# Dictionary

The Dictionary class is defined in the System.Collections.Generics namespace. It stores the key-value pair and implements the Implements IDictionary<TKey, TValue> interface. The key in a Dictionary is unique and can never be null. The result will not be sorted by keys, unlike the SortedList Collection.

Create a Dictionary

To create a Dictionary with an int key and string value pair, use <int, string>. Set the type in angular braces as shown below:

Dictionary<int, string> dict = new Dictionary<int, string>();

Let us now see an example of creating a Dictionary in C# with string elements:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       Dictionary<int, string> dict = new Dictionary<int, string>();
       dict.Add(4,"Badminton");
       dict.Add(1,"Cricket");
       dict.Add(5,"Golf");
       dict.Add(7,"VolleyBall");
       dict.Add(2,"Football");
       dict.Add(3,"Tennis");
       dict.Add(6,"Hockey");

       Console.WriteLine("\nDictionary...");
       foreach (KeyValuePair<int, string> i in dict)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }     
     }
  }
}

Output

Dictionary...
Key: 4, Value: Badminton
Key: 1, Value: Cricket
Key: 5, Value: Golf
Key: 7, Value: VolleyBall
Key: 2, Value: Football
Key: 3, Value: Tennis
Key: 6, Value: Hockey

Remove all the keys and values

To remove all the keys and values from a Dictionary, use the Clear() method. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       Dictionary<int, string> dict = new Dictionary<int, string>();
       dict.Add(4,"Badminton");
       dict.Add(1,"Cricket");
       dict.Add(5,"Golf");
       dict.Add(7,"VolleyBall");
       dict.Add(2,"Football");
       dict.Add(3,"Tennis");
       dict.Add(6,"Hockey");

       Console.WriteLine("\nDictionary...");
       foreach (KeyValuePair<int, string> i in dict)
       {
          Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       // Display the count of Dictionary elements
       Console.WriteLine("Count = "+dict.Count);
       
       // Clearing the elements
       dict.Clear();
       Console.WriteLine("\nRemoving all the elements...");
       Console.WriteLine("Count (Updated) = "+dict.Count);
   }
  }
}

Output

Dictionary...
Key: 4, Value: Badminton
Key: 1, Value: Cricket
Key: 5, Value: Golf
Key: 7, Value: VolleyBall
Key: 2, Value: Football
Key: 3, Value: Tennis
Key: 6, Value: Hockey
Count = 7

Removing all the elements...
Count (Updated) = 0

Display only the keys

Use the Keys property to display only the keys from a Dictionary in C#. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       Dictionary<int, string> dict = new Dictionary<int, string>();
       dict.Add(4,"Badminton");
       dict.Add(1,"Cricket");
       dict.Add(5,"Golf");
       dict.Add(7,"VolleyBall");
       dict.Add(2,"Football");
       dict.Add(3,"Tennis");
       dict.Add(6,"Hockey");

       Console.WriteLine("\nDictionary...");
       foreach (KeyValuePair<int, string> i in dict)
       {
          Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
      Console.WriteLine("\nDictionary Keys...");
        
      List<int> k = new List<int>(dict.Keys);
       foreach (int keys in k)
       {
          Console.WriteLine(keys);
       }
   }
  }
}

Output

Dictionary...
Key: 4, Value: Badminton
Key: 1, Value: Cricket
Key: 5, Value: Golf
Key: 7, Value: VolleyBall
Key: 2, Value: Football
Key: 3, Value: Tennis
Key: 6, Value: Hockey

Dictionary Keys...
4
1
5
7
2
3
6

Display only the values

Use the Values property to display only the values from a Dictionary in C#. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       Dictionary<int, string> dict = new Dictionary<int, string>();
       dict.Add(4,"Badminton");
       dict.Add(1,"Cricket");
       dict.Add(5,"Golf");
       dict.Add(7,"VolleyBall");
       dict.Add(2,"Football");
       dict.Add(3,"Tennis");
       dict.Add(6,"Hockey");

       Console.WriteLine("\nDictionary...");
       foreach (KeyValuePair<int, string> i in dict)
       {
          Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
      Console.WriteLine("\nDictionary Values...");
        
      List<string> v = new List<string>(dict.Values);
       foreach (string s in v)
       {
          Console.WriteLine(s);
       }
   }
  }
}

Output

Dictionary...
Key: 4, Value: Badminton
Key: 1, Value: Cricket
Key: 5, Value: Golf
Key: 7, Value: VolleyBall
Key: 2, Value: Football
Key: 3, Value: Tennis
Key: 6, Value: Hockey

Dictionary Values...
Badminton
Cricket
Golf
VolleyBall
Football
Tennis
Hockey

Check the Dictionary for a specific key

The ContainsKey() method allows you to check a Dictionary for a key. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       Dictionary<int, string> dict = new Dictionary<int, string>();
       dict.Add(4,"Badminton");
       dict.Add(1,"Cricket");
       dict.Add(5,"Golf");
       dict.Add(7,"VolleyBall");
       dict.Add(2,"Football");
       dict.Add(3,"Tennis");
       dict.Add(6,"Hockey");

       Console.WriteLine("\nDictionary...");
       foreach (KeyValuePair<int, string> i in dict)
       {
          Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nKey 2 is present? "+dict.ContainsKey(2));
   }
  }
}

Output

Dictionary...
Key: 4, Value: Badminton
Key: 1, Value: Cricket
Key: 5, Value: Golf
Key: 7, Value: VolleyBall
Key: 2, Value: Football
Key: 3, Value: Tennis
Key: 6, Value: Hockey

Key 2 is present? True

Check the Dictionary for a specific value

The ContainsValue() method allows you to check a Dictionary for a value. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       Dictionary<int, string> dict = new Dictionary<int, string>();
       dict.Add(4,"Badminton");
       dict.Add(1,"Cricket");
       dict.Add(5,"Golf");
       dict.Add(7,"VolleyBall");
       dict.Add(2,"Football");
       dict.Add(3,"Tennis");
       dict.Add(6,"Hockey");

       Console.WriteLine("\nDictionary...");
       foreach (KeyValuePair<int, string> i in dict)
       {
          Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nValue Tennis is present? "+dict.ContainsValue("Tennis"));
   }
  }
}

Output

Dictionary...
Key: 4, Value: Badminton
Key: 1, Value: Cricket
Key: 5, Value: Golf
Key: 7, Value: VolleyBall
Key: 2, Value: Football
Key: 3, Value: Tennis
Key: 6, Value: Hockey

Value Tennis is present? True

Read More

C# SortedList
C# Tuple
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment