Dictionary Methods in C#

Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collections.Generic namespace and provides methods to add, remove, and search for key-value pairs efficiently.

Common Dictionary Methods

Method Description
Add(TKey, TValue) Adds a key-value pair to the Dictionary
Remove(TKey) Removes the element with the specified key
Clear() Removes all keys and values from the Dictionary
ContainsKey(TKey) Checks whether the specified key exists
ContainsValue(TValue) Checks whether the specified value exists
TryGetValue(TKey, out TValue) Safely retrieves a value by key without throwing exceptions
Count Gets the number of key-value pairs (property, not method)

Using Add() and Count

The Add()Count property returns the total number of elements −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      IDictionary<int, int> d = new Dictionary<int, int>();
      d.Add(1, 97);
      d.Add(2, 89);
      d.Add(3, 77);
      d.Add(4, 88);
      d.Add(5, 78);
      d.Add(6, 98);
      Console.WriteLine("Total elements: " + d.Count);
   }
}

The output of the above code is −

Total elements: 6

Using ContainsKey() and ContainsValue()

These methods check for the existence of keys and values respectively −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, int> ages = new Dictionary<string, int>();
      ages.Add("Alice", 25);
      ages.Add("Bob", 30);
      ages.Add("Charlie", 35);
      
      Console.WriteLine("Contains key 'Alice': " + ages.ContainsKey("Alice"));
      Console.WriteLine("Contains key 'David': " + ages.ContainsKey("David"));
      Console.WriteLine("Contains value 30: " + ages.ContainsValue(30));
      Console.WriteLine("Contains value 40: " + ages.ContainsValue(40));
   }
}

The output of the above code is −

Contains key 'Alice': True
Contains key 'David': False
Contains value 30: True
Contains value 40: False

Using TryGetValue() for Safe Access

The TryGetValue() method safely retrieves values without throwing exceptions if the key doesn't exist −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, int> scores = new Dictionary<string, int>();
      scores.Add("Math", 95);
      scores.Add("Science", 88);
      scores.Add("English", 92);
      
      int value;
      if (scores.TryGetValue("Math", out value)) {
         Console.WriteLine("Math score: " + value);
      }
      
      if (scores.TryGetValue("History", out value)) {
         Console.WriteLine("History score: " + value);
      } else {
         Console.WriteLine("History score not found");
      }
   }
}

The output of the above code is −

Math score: 95
History score not found

Using Remove() and Clear()

The Remove() method removes individual elements, while Clear() removes all elements −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, string> capitals = new Dictionary<string, string>();
      capitals.Add("USA", "Washington");
      capitals.Add("UK", "London");
      capitals.Add("France", "Paris");
      
      Console.WriteLine("Initial count: " + capitals.Count);
      
      capitals.Remove("UK");
      Console.WriteLine("After removing UK: " + capitals.Count);
      
      capitals.Clear();
      Console.WriteLine("After clearing all: " + capitals.Count);
   }
}

The output of the above code is −

Initial count: 3
After removing UK: 2
After clearing all: 0

Conclusion

Dictionary methods in C# provide efficient ways to manage key-value collections. Use Add() and Remove() for modifications, ContainsKey() and TryGetValue() for safe lookups, and Clear() for bulk operations. These methods make Dictionary a powerful data structure for fast key-based access.

Updated on: 2026-03-17T07:04:35+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements