HybridDictionary Class

System.Collections.Specialized

Represents a dictionary that uses a Hashtable by default, but switches to an ArrayList when the number of elements reaches a certain threshold.

Syntax


[SerializableAttribute]
public class HybridDictionary : ICloneable, IDictionary, ICollection, IEnumerable
            

Inheritance

Object > HybridDictionary

Remarks

The HybridDictionary class is useful when you need a dictionary that performs well for both small and large numbers of entries. It starts with a highly efficient Hashtable for low counts and automatically transitions to an ArrayList when performance with a Hashtable degrades significantly due to the number of entries.

The threshold at which the dictionary switches from Hashtable to ArrayList is determined by the HybridDictionary.HybridDictionary() constructor. The default threshold is 10 elements.

This class is not thread-safe. If you need thread-safe access to the HybridDictionary collection, use the methods of the Collections.Synchronized method.

Constructors

Name Description
HybridDictionary() Initializes a new instance of the HybridDictionary class that is empty, has the default initial capacity, and uses the default load factor and case-insensitive comparer.
Uses a Hashtable with a threshold of 10.
HybridDictionary(bool caseInsensitive) Initializes a new instance of the HybridDictionary class that is empty and has the default initial capacity, using the specified case sensitivity.
Uses a Hashtable with a threshold of 10.
HybridDictionary(int initialSize) Initializes a new instance of the HybridDictionary class that is empty and has the specified initial capacity, using the default load factor and case-insensitive comparer.
Uses a Hashtable with a threshold of 10.
HybridDictionary(int initialSize, bool caseInsensitive) Initializes a new instance of the HybridDictionary class that is empty and has the specified initial capacity, using the specified case sensitivity and load factor.
Uses a Hashtable with a threshold of 10.

Fields

No public static fields.

Properties

Name Description
Count Gets the number of key/value pairs contained in the HybridDictionary.
IsFixedSize Gets a value indicating whether the HybridDictionary has a fixed size.
IsReadOnly Gets a value indicating whether the HybridDictionary is read-only.
Keys Gets an ICollection containing the keys of the HybridDictionary.
Values Gets an ICollection containing the values of the HybridDictionary.
Item[Object key] Gets or sets the value associated with the specified key.

Methods

Name Description
Add(Object key, Object value) Adds an element with the specified key and value to the HybridDictionary.
Clear() Removes all elements from the HybridDictionary.
Clone() Creates a shallow copy of the HybridDictionary.
Contains(Object key) Determines whether the HybridDictionary contains the specified key.
GetEnumerator() Returns an enumerator that iterates through the HybridDictionary.
Remove(Object key) Removes the element with the specified key from the HybridDictionary.

Example

C# Example


using System;
using System.Collections.Specialized;

public class HybridDictionaryExample
{
    public static void Main(string[] args)
    {
        // Initialize a HybridDictionary with default settings (threshold 10)
        HybridDictionary dict = new HybridDictionary();

        // Add some elements
        dict.Add("Key1", "Value1");
        dict.Add("Key2", "Value2");
        dict.Add("Key3", "Value3");

        Console.WriteLine($"Dictionary contains {dict.Count} elements.");

        // Access an element
        Console.WriteLine($"Value for Key2: {dict["Key2"]}");

        // Remove an element
        dict.Remove("Key1");
        Console.WriteLine($"Dictionary contains {dict.Count} elements after removal.");

        // Demonstrate case-insensitivity if initialized that way
        HybridDictionary caseInsensitiveDict = new HybridDictionary(true);
        caseInsensitiveDict.Add("Apple", 1);
        caseInsensitiveDict.Add("apple", 2); // This would overwrite if case-sensitive
        Console.WriteLine($"Case-insensitive dictionary count: {caseInsensitiveDict.Count}");
        Console.WriteLine($"Value for 'APPLE': {caseInsensitiveDict["APPLE"]}");
    }
}
            

Requirements

Namespace: System.Collections.Specialized

Assembly: System.Collections.dll