SortedDictionary<TKey, TValue> Class

Namespace: System.Collections.Generic

Assembly: System.Private.CoreLib.dll

Represents a collection of key/value pairs that are sorted by the keys and are available through an index.

Implements

  • IDictionary<TKey, TValue>
  • ICollection<KeyValuePair<TKey, TValue>>
  • IEnumerable<KeyValuePair<TKey, TValue>>
  • IReadOnlyDictionary<TKey, TValue>
  • IReadOnlyCollection<KeyValuePair<TKey, TValue>>
  • IEnumerable
  • ICollection
  • IDictionary
  • ICloneable

Inheritance

  • Object
  • SortedDictionary<TKey, TValue>

Constructors

public SortedDictionary<TKey, TValue>()

Initializes a new instance of the SortedDictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the default comparer for the key type.

public SortedDictionary<TKey, TValue>(IComparer<TKey> comparer)

Initializes a new instance of the SortedDictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the specified comparer.

public SortedDictionary<TKey, TValue>(IDictionary<TKey, TValue> dictionary)

Initializes a new instance of the SortedDictionary<TKey, TValue> class that contains elements copied from the specified dictionary, has the default initial capacity, and uses the default comparer for the key type.

public SortedDictionary<TKey, TValue>(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)

Initializes a new instance of the SortedDictionary<TKey, TValue> class that contains elements copied from the specified dictionary, has the default initial capacity, and uses the specified comparer.

public SortedDictionary<TKey, TValue>(int capacity)

Initializes a new instance of the SortedDictionary<TKey, TValue> class that is empty, has the specified initial capacity, and uses the default comparer for the key type.

public SortedDictionary<TKey, TValue>(int capacity, IComparer<TKey> comparer)

Initializes a new instance of the SortedDictionary<TKey, TValue> class that is empty, has the specified initial capacity, and uses the specified comparer.

Properties

public IComparer<TKey> Comparer { get; }

Gets the comparer that is used to order the elements of the SortedDictionary<TKey, TValue>. This property is null if the default comparer is used.

public int Count { get; }

Gets the number of key/value pairs contained in the SortedDictionary<TKey, TValue>.

public TValue Item { get; set; }

Gets or sets the value associated with the specified key.

public SortedDictionary<TKey, TValue>.KeyCollection Keys { get; }

Gets a collection that contains the keys in the SortedDictionary<TKey, TValue>. The keys are in order.

public SortedDictionary<TKey, TValue>.ValueCollection Values { get; }

Gets a collection that contains the values in the SortedDictionary<TKey, TValue>. The values are in order of the corresponding keys.

Methods

public void Add(TKey key, TValue value)

Adds an element with the specified key and value to the SortedDictionary<TKey, TValue> with the specified key and value.

public bool ContainsKey(TKey key)

Determines whether the SortedDictionary<TKey, TValue> contains the specified key.

public bool ContainsValue(TValue value)

Determines whether the SortedDictionary<TKey, TValue> contains the specified value.

public void Clear()

Removes all elements from the SortedDictionary<TKey, TValue>.

public void GetObjectData(SerializationInfo info, StreamingContext context)

Implements the ISerializable interface and applies a custom attribute to the SortedDictionary<TKey, TValue> instance.

public bool Remove(TKey key)

Removes the element with the specified key from the SortedDictionary<TKey, TValue>.

public bool TryAdd(TKey key, TValue value)

Attempts to add the specified key and value to the dictionary. If the dictionary already contains the specified key, this method returns false.

public bool TryGetValue(TKey key, out TValue value)

Gets the value associated with the specified key. If the key is not found, the value is set to the default value of the type of the value parameter.

Example


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a SortedDictionary with string keys and int values.
        SortedDictionary<string, int> sd = new SortedDictionary<string, int>();

        // Add elements to the dictionary.
        sd.Add("Apple", 5);
        sd.Add("Banana", 2);
        sd.Add("Orange", 8);
        sd.Add("Grape", 12);

        // Display the elements of the dictionary.
        Console.WriteLine("Dictionary elements:");
        foreach (KeyValuePair<string, int> kvp in sd)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        }

        // Access a value by key.
        Console.WriteLine("\nValue for 'Orange': {0}", sd["Orange"]);

        // Check if a key exists.
        if (sd.ContainsKey("Banana"))
        {
            Console.WriteLine("\nDictionary contains key 'Banana'.");
        }

        // Remove an element.
        sd.Remove("Grape");
        Console.WriteLine("\nRemoved 'Grape'.");

        // Display the elements again.
        Console.WriteLine("\nDictionary elements after removal:");
        foreach (KeyValuePair<string, int> kvp in sd)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        }
    }
}
            

Generic Type Parameters

  • TKey
  • TValue

Remarks

The SortedDictionary<TKey, TValue> is a generic collection that stores key/value pairs. It is a type of dictionary that maintains its elements in ascending order by key. This ordering is based on the default comparer for the key type or a comparer provided when the dictionary is created. The keys in a SortedDictionary<TKey, TValue> must be unique.

The SortedDictionary<TKey, TValue> implements several interfaces, including IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, and IEnumerable<KeyValuePair<TKey, TValue>>. It also provides read-only views through IReadOnlyDictionary<TKey, TValue> and IReadOnlyCollection<KeyValuePair<TKey, TValue>>.

When adding or removing elements, the SortedDictionary<TKey, TValue> automatically reorders itself to maintain the sorted order. This can make operations such as adding or removing elements slightly more computationally intensive compared to a hash table-based dictionary like Dictionary<TKey, TValue>.