Microsoft Learn

Dictionary<TKey, TValue> Class

Namespace: System.Collections.Generic
Assembly: System.Private.CoreLib

Summary

Represents a collection of key/value pairs that are ordered based on the key. Each key must be unique and cannot be null.

Type parameters:

  • TKey: The type of the keys in the dictionary.
  • TValue: The type of the values in the dictionary.

Syntax

public class Dictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>

Constructors

Dictionary()
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 type of the keys.
Dictionary(int capacity)
Initializes a new instance of the Dictionary<TKey, TValue> class that is empty and has the specified initial capacity, and uses the default equality comparer for the type of the keys.
Dictionary(IEqualityComparer<TKey> comparer)
Initializes a new instance of the Dictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the specified IEqualityComparer<TKey>.

Properties

int Count { get; }
Gets the number of elements contained in the Dictionary<TKey, TValue>.
TKey Dictionary<TKey, TValue>.Item[TKey key] { get; set; }
Gets or sets the element with the specified key.

Methods

void Add(TKey key, TValue value)
Adds an element with the specified key and value to the dictionary.
bool ContainsKey(TKey key)
Determines whether the dictionary contains an element with the specified key.
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.

Example

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a dictionary
        Dictionary<string, int> ages = new Dictionary<string, int>()
        {
            { "Alice", 30 },
            { "Bob", 25 },
            { "Charlie", 35 }
        };

        // Add a new entry
        ages.Add("David", 28);

        // Access a value
        Console.WriteLine($"Alice's age: {ages["Alice"]}");

        // Check if a key exists
        if (ages.ContainsKey("Bob"))
        {
            Console.WriteLine("Bob is in the dictionary.");
        }

        // Iterate through the dictionary
        Console.WriteLine("\nAll entries:");
        foreach (KeyValuePair<string, int> kvp in ages)
        {
            Console.WriteLine($"- {kvp.Key}: {kvp.Value}");
        }

        // Try to get a value safely
        if (ages.TryGetValue("Eve", out int eveAge))
        {
            Console.WriteLine($"Eve's age: {eveAge}");
        }
        else
        {
            Console.WriteLine("Eve is not in the dictionary.");
        }
    }
}

See Also

Reference Description
System.Collections.Generic Namespace Contains interfaces and classes that define generic collections, which allow developers to create collections that protect against adding incorrect types to the collection.
Microsoft.VisualBasic.CompilerServices.DictionaryBase Class Represents a strongly typed collection of key and value pairs.