C# | Creating a HybridDictionary with specified initial size & case sensitivity

HybridDictionary(Int32, Boolean) creates a HybridDictionary with the specified initial size and case sensitivity.

Syntax:

public HybridDictionary (int initialSize, bool caseInsensitive);

Parameters:



  • initialSize : The approximate number of entries that the HybridDictionary can initially contain.
  • caseInsensitive : A Boolean that denotes whether the HybridDictionary is case-insensitive.

Below programs illustrate the use of HybridDictionary(Int32, Boolean):

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a HybridDictionary
// with the specified initial size
// and case sensitivity.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary with the
        // specified initial size and case sensitivity.
        HybridDictionary myDict = new HybridDictionary(10, false);
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
  
        // This will not raise exception as the
        // Collection is not case-insensitive
        myDict.Add("i", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // Displaying the key/value pairs in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " " + de.Value);
    }
}

chevron_right


Output:

III third
V fifth
II second
i first
I first
IV fourth

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to create a HybridDictionary
// with the specified initial size
// and case sensitivity.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary with the
        // specified initial size and case sensitivity.
        HybridDictionary myDict = new HybridDictionary(10, true);
  
        // Adding key/value pairs in myDict
        myDict.Add("A", "Apple");
  
        // This will raise exception as the
        // Collection is case-insensitive
        myDict.Add("a", "Air");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        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


Runtime Error:

Unhandled Exception:
System.ArgumentException: Item has already been added. Key in dictionary: ‘A’ Key being added: ‘a’
at System.Collections.Hashtable.Insert

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.
  • 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.