Represents a collection of key/value pairs that are sorted by the key. This collection is generic and can store any type of key and value.
Namespace: System.Collections.Generic
Assembly: System.Private.CoreLib
A Dictionary<TKey, TValue>
is a generic collection that provides a way to store, retrieve, and manage items using unique keys.
It offers efficient O(1) average time complexity for adding, removing, and looking up elements, provided the hash function is implemented properly.
Thread Safety:
A public static (Shared
in Visual Basic) member of this type is thread-safe. Any instance member is not guaranteed to be thread-safe.
Object
→ Dictionary<TKey, TValue>
Dictionary<TKey, TValue>
implements the following interfaces:
ICollection<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>
IDictionary<TKey, TValue>
ICollection
IEnumerable
IDictionary
Name | Description |
---|---|
Count |
Gets the number of key/value pairs contained in the Dictionary<TKey, TValue>. |
Item[TKey key] |
Gets or sets the value associated with the specified key. |
Keys |
Gets a collection of the keys contained in the Dictionary<TKey, TValue>. |
Values |
Gets a collection of the values contained in the Dictionary<TKey, TValue>. |
Name | Description |
---|---|
Add(TKey key, TValue value) |
Adds an element with the specified key and value to the Dictionary<TKey, TValue>. |
Clear() |
Removes all keys and values from the Dictionary<TKey, TValue>. |
ContainsKey(TKey key) |
Determines whether the Dictionary<TKey, TValue> contains an element with the specified key. |
ContainsValue(TValue value) |
Determines whether the Dictionary<TKey, TValue> contains an element with the specified value. |
Equals(object obj) |
Determines whether the specified object is equal to the current object. |
GetEnumerator() |
Returns an enumerator that iterates through the Dictionary<TKey, TValue>. |
GetHashCode() |
Serves as the default hash function. |
GetType() |
Gets the Type of the current instance. |
Remove(TKey key) |
Removes the element with the specified key from the Dictionary<TKey, TValue>. |
Remove(TKey key, out TValue value) |
Removes the element with the specified key from the Dictionary<TKey, TValue> and copies the specified element to the value parameter. |
ToString() |
Returns a string that represents the current object. |
TryAdd(TKey key, TValue value) |
Attempts to add the specified key and value to the dictionary. |
TryGetValue(TKey key, out TValue value) |
Gets the value associated with the specified key. |
The following example demonstrates how to create, populate, and iterate through a Dictionary<TKey, TValue>
.
using System;
using System.Collections.Generic;
public class DictionaryExample
{
public static void Main(string[] args)
{
// Create a dictionary with string keys and int values.
Dictionary<string, int> cities = new Dictionary<string, int>();
// Add elements to the dictionary.
cities.Add("New York", 8419000);
cities.Add("London", 8900000);
cities.Add("Paris", 2141000);
cities.Add("Tokyo", 13929000);
// Access values by key.
Console.WriteLine($"The population of Paris is: {cities["Paris"]}");
// Check if a key exists.
if (cities.ContainsKey("London"))
{
Console.WriteLine("London is in the dictionary.");
}
// Iterate through the dictionary.
Console.WriteLine("\nIterating through the dictionary:");
foreach (KeyValuePair<string, int> kvp in cities)
{
Console.WriteLine($"City: {kvp.Key}, Population: {kvp.Value}");
}
// Remove an element.
cities.Remove("Tokyo");
Console.WriteLine("\nRemoved Tokyo. Dictionary count: " + cities.Count);
// Try to add a duplicate key (will throw an exception).
try
{
cities.Add("New York", 8500000);
}
catch (ArgumentException e)
{
Console.WriteLine($"\nError adding duplicate key: {e.Message}");
}
// TryGetValue example
if (cities.TryGetValue("London", out int londonPopulation))
{
Console.WriteLine($"\nSuccessfully retrieved population for London: {londonPopulation}");
}
else
{
Console.WriteLine("\nLondon not found in the dictionary.");
}
}
}