C# | Creating a Case-Sensitive HybridDictionary with specified initial size

HybridDictionary(Int32) constructor is used to create a case-sensitive HybridDictionary with the specified initial size.

Syntax:

public HybridDictionary (int initialSize);

Here, initialSize is the approximate number of entries that the HybridDictionary can initially contain.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a case-sensitive
// HybridDictionary with the specified
// initial size.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a case-sensitive HybridDictionary
        // with the specified initial size.
        HybridDictionary myDict = new HybridDictionary(10);
  
        // Adding key/value pairs in myDict
        myDict.Add("A", "Apple");
  
        // This will not raise exception as
        // By default, the collection is case-sensitive
        myDict.Add("a", "Air");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
  
        // This will not raise exception as
        // By default, the collection is case-sensitive
        myDict.Add("d", "Dolphine");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " " + de.Value);
    }
}

chevron_right


Output:

B Banana
a Air
A Apple
d Dolphine
C Cat
E Elephant
F Fish
D Dog

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a case-sensitive
// HybridDictionary with the specified
// initial size.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a case-sensitive HybridDictionary
        // with the specified initial size.
        HybridDictionary myDict = new HybridDictionary(10);
  
        // Adding key/value pairs in myDict
        // As the HybridDictionary is case-sensitive
        // Therefore no exception is raised
        // Because "k1" & "K1" are taken as different keys
        // Similarly "k2" & "K2", "k3" & "K3"
        myDict.Add("k1", "v1");
        myDict.Add("K1", "v1");
        myDict.Add("k2", "v2");
        myDict.Add("K2", "v2");
        myDict.Add("k3", "v3");
        myDict.Add("K3", "v3");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " " + de.Value);
    }
}

chevron_right


Output:

k3 v3
K3 v3
k2 v2
K2 v2
k1 v1
K1 v1

Note:

  • If the initial size of the collection is greater than the optimal size for a ListDictionary, the collection is stored in a Hashtable to avoid the overhead of copying elements from the ListDictionary to the Hashtable.
  • By default, the collection is case-sensitive.
  • Every key in a HybridDictionary must be unique.
  • This constructor is an O(n) operation, where n is initialSize.


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet