Last Updated : 11 Jul, 2025
Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature means the size of the dictionary is growing according to the need.
Example 1: Creating and Displaying a Dictionary
C#
// C# program to demonstrate how to
// create and display a dictionary
using System;
using System.Collections.Generic;
class Geeks
{
public static void Main()
{
// Creating a dictionary
Dictionary<int, string> sub = new Dictionary<int, string>();
// Adding elements
sub.Add(1, "C#");
sub.Add(2, "Javascript");
sub.Add(3, "Dart");
// Displaying dictionary
foreach (var ele in sub)
{
Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
}
}
}
Key: 1, Value: C# Key: 2, Value: Javascript Key: 3, Value: DartSteps to Create a Dictionary
The dictionary can be created in different ways using Dictionary Class. Here, we are using the Dictionary<TKey, TValue>() constructor used to create an instance of the Dictionary<TKey, TValue> class by default initial capacity is empty, and uses the default equality comparison for the key type.
Step 1: Include System.Collections.Generic namespace
using System.Collections.Generic;
Step 2: Create a Dictionary using Dictionary<TKey, TValue> class
Performing Different Operations on Dictionary 1. Adding ElementsDictionary dictionary_name = new Dictionary();
2. Accessing Elements// Using Add method
Dictionary<int, string> dict= new Dictionary<int, string>();
dict.Add(1, "One");// Using Collection Initializer
Dictionary<int, string> dict = new Dictionary<int, string>
{
{ 1, "One" },
{ 2, "Two" }
};// Using Indexers
Dictionary<int, string> dict = new Dictionary<int, string>();
dict[1] = "One";
dict[2] = "Two";
The key-value pair of the Dictionary is accessed using three different ways:
Using For Loop: We can use a to access the key-value pairs of the Dictionary.
for(int =0; x< dict.Count; x++)
{
Console.WriteLine("{0} and {1}", dict.Keys.ElementAt(x
dict[ dict.Keys.ElementAt(x)]);
}
Using Index: We can access individual key-value pairs of the Dictionary by using its index value. We can specify the key in the index to get the value from the given dictionary, no need to specify the index. The indexer always takes the key as a parameter
Console.WriteLine("Value is:{0}", dict[1]);
Console.WriteLine("Value is:{0}", dict[2]);
Note: If the given key is not available in the dictionary, then it gives KeyNotFoundException.
Using foreach loop:
foreach (var pair in dict)
Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}");
Example: Creating and Displaying a Dictionary with Add()
C#
// C# program to illustrate how to
// create a dictionary using Add()
using System;
using System.Collections.Generic;
class Geeks
{
static public void Main()
{
// Creating a dictionary using Dictionary<TKey,TValue> class
Dictionary<int, string> dict = new Dictionary<int, string>();
// Adding key-Value pairs
dict.Add(1, "Welcome");
dict.Add(2, "to");
dict.Add(3, "GeeksforGeeks");
// Displaying the dictionary
foreach (KeyValuePair<int, string> ele in dict)
{
Console.WriteLine("key: {0} and value: {1}", ele.Key, ele.Value);
}
}
}
key: 1 and value: Welcome key: 2 and value: to key: 3 and value: GeeksforGeeks3. Removing Elements
In the Dictionary, we are allowed to remove elements from the Dictionary. Dictionary<TKey, TValue> class provides two different methods to remove elements that are:
Example:
C#
// Removing key-value pairs from the dictionary
using System;
using System.Collections.Generic;
class Geeks
{
static public void Main()
{
// Creating a dictionary
Dictionary<int, string> dict = new Dictionary<int, string>();
// Adding key-value pairs
dict.Add(1, "Welcome");
dict.Add(2, "to");
dict.Add(3, "GeeksforGeeks");
// Before Remove() method
foreach (KeyValuePair<int, string> ele in dict)
{
Console.WriteLine("key: {0}, Value: {1}", ele.Key, ele.Value);
}
// Remove a key-value pair
dict.Remove(1);
Console.WriteLine("\nAfter Remove() method:");
foreach (KeyValuePair<int, string> ele in dict)
{
Console.WriteLine("key: {0}, Value: {1}", ele.Key, ele.Value);
}
}
}
key: 1, Value: Welcome key: 2, Value: to key: 3, Value: GeeksforGeeks After Remove() method: key: 2, Value: to key: 3, Value: GeeksforGeeks4. Checking Element
In Dictionary, we can check whether the given key or value is present in the specified dictionary or not. The Dictionary<TKey, TValue> class provides two different methods which are:
Example:
C#
// Checking if a key or value is present in the dictionary
using System;
using System.Collections.Generic;
class Geeks
{
static public void Main()
{
// Creating a dictionary
Dictionary<int, string> dict = new Dictionary<int, string>();
// Adding key-value pairs
dict.Add(1, "Welcome");
dict.Add(2, "to");
dict.Add(3, "GeeksforGeeks");
// Using ContainsKey() to check if the key exists
if (dict.ContainsKey(1))
Console.WriteLine("Key is found...!!");
else
Console.WriteLine("Key is not found...!!");
// Using ContainsValue() to check if the value exists
if (dict.ContainsValue("to"))
Console.WriteLine("Value is found...!!");
else
Console.WriteLine("Value is not found...!!");
}
}
Key is found...!! Value is found...!!Important Points
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4