Last Updated : 11 Jul, 2025
In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value.
Example: This example, demonstrates how to create a Dictionary, insert key-value pairs and iterate through them to display the keys and their corresponding values.
C#
// C# Program to add elements in a Dictionary
using System;
using System.Collections.Generic;
class Geeks
{
static void Main()
{
// Create a dictionary to store key-value pairs
Dictionary<string, int> d = new Dictionary<string, int>();
// Insert elements into the dictionary
d.Add("Geek1", 1);
d.Add("Geek2", 2);
d.Add("Geek3", 3);
// Display all key-value pairs in the dictionary
foreach (var i in d)
{
Console.WriteLine($"key: {i.Key}, value: {i.Value}");
}
}
}
key: Geek1, value: 1 key: Geek2, value: 2 key: Geek3, value: 3Declarartion of Dictionary
In C#, the declaration of Dictionary can be done as:
Dictionary<TKey, TValue> dictionaryName = new Dictionary<TKey, TValue>();
Parameters:
Note: Tkey is the type of the keys and Tvalue is the type of the values
Since the Dictionary<TKey, TValue> is a collection of keys and values, the element type is not the type of the key or the type of the value. Instead, the element type is a KeyValuePair <TKey, TValue> of the key type and the value type.
Constructors Constructors Description Dictionary<TKey, TValue>() Initializes a new instance of the Dictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the default equality comparer for the key type.Example: This example demonstrates the count of pairs and displaying all the key-value pairs.
C#
// C# Program to create a Dictionary
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Create a new dictionary of
// strings, with string Keys
Dictionary<string, string> d =
new Dictionary<string, string>();
// Adding key/value pairs in d
d.Add("1", "C");
d.Add("2", "C++");
d.Add("3", "Java");
d.Add("4", "Python");
// To get count of key-value pairs in dictionary
Console.WriteLine("Total no of pairs"+
" in Dictionary are: " + d.Count);
// Displaying the key-value pairs in dictionary
Console.WriteLine("\nThe key-value pairs"+
" in Dictionary are: ");
foreach(KeyValuePair<string, string> i in d)
{
Console.WriteLine("Key = {0}, Value = {1}",
i.Key, i.Value);
}
}
}
Total no of pairs in Dictionary are: 4 The key-value pairs in Dictionary are: Key = 1, Value = C Key = 2, Value = C++ Key = 3, Value = Java Key = 4, Value = PythonProperties Properties Description Comparer Gets the IEqualityComparer<TKey, TValue> that is used to determine equality of keys for the dictionary. Count Gets the number of key/value pairs contained in the Dictionary<TKey, TValue>. Item[TKey] Gets or sets the value associated with the specified key. Keys Gets a collection containing the keys in the Dictionary<TKey, TValue>. Values Gets a collection containing the values in the Dictionary<TKey, TValue>.
Example 1: This program demonstrates how to retrieve and display all the keys from the dictionary.
C#
// C# Program to get the keys
// in the Dictionary
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Create a new dictionary of
// strings, with string keys
Dictionary<string, string> d
= new Dictionary<string, string>();
// Adding key/value pairs
d.Add("1", "C");
d.Add("2", "C++");
d.Add("3", "Java");
d.Add("4", "Python");
// To get count of key-value pairs
Console.WriteLine("Total key-value pairs are: "
+ d.Count);
// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl
= d.Keys;
// The elements of the KeyCollection
// are strongly typed with the type
// that was specified for dictionary keys
foreach(string s in keyColl)
{
Console.WriteLine("Key = {0}", s);
}
}
}
Total key-value pairs are: 4 Key = 1 Key = 2 Key = 3 Key = 4
Example 2: This program demonstrates how to retrieve and display all the values from the dictionary.
C#
// C# Program to get the values
// in the Dictionary
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Create a new dictionary of
// strings, with string keys
Dictionary<string, string> d =
new Dictionary<string, string>();
// Adding key-value pairs
d.Add("1", "C");
d.Add("2", "C++");
d.Add("3", "Java");
d.Add("4", "Python");
// To get count of key-value pairs
Console.WriteLine("Total key-value pairs are: "
+ d.Count);
// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
d.Values;
// The elements of the ValueCollection
// are strongly typed with the type
// that was specified for dictionary values.
foreach(string s in valueColl)
{
Console.WriteLine("Value = {0}", s);
}
}
}
Total key-value pairs are: 4 Value = C Value = C++ Value = Java Value = PythonMethods Method Description Add(TKey, TValue) Adds the specified key and value to the dictionary.
Example 1: This example demonstrates how to use clear() to remove all the elements from the dictionary.
C#
// C# Program to remove all entries
// from Dictionary
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Create a new dictionary of
// strings, with string keys
Dictionary<string, string> d
= new Dictionary<string, string>();
// Adding key/value pairs
d.Add("1", "C");
d.Add("2", "C++");
d.Add("3", "Java");
d.Add("4", "Python");
// To get count of key-value pairs
Console.WriteLine("Total key-value pairs: "
+ d.Count);
d.Clear();
// To get count of key-value pairs
Console.WriteLine(
"Total key-value pairs after Clear operation: "
+ d.Count);
}
}
Total key-value pairs: 4 Total key-value pairs after Clear operation: 0
Example 2: This example demonstrates how to remove entries based on their keys.
C#
// C# code to remove the entry with
// the specified key from the Dictionary
using System;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
// Create a new dictionary of
// strings, with string keys
Dictionary<string, string> d =
new Dictionary<string, string>();
// Adding key/value pairs in d
d.Add("1", "C");
d.Add("2", "C++");
d.Add("3", "Java");
d.Add("4", "Python");
// To get count of key/value pairs
Console.WriteLine("Total key-value pairs: "
+ d.Count);
// Remove the entry with the
// specified key from the Dictionary
d.Remove("1");
d.Remove("3");
// To get count of key-value pairs
Console.WriteLine("Total key-value pairs after removal: "
+ d.Count);
}
}
Total key-value pairs: 4 Total key-value pairs after removal: 2
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