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


To create a case-sensitive HybridDictionary with specified initial size, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary myDict = new HybridDictionary(5, false);
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("D", "FG");
      myDict.Add("e", "fg");
      Console.WriteLine("Key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
      Console.WriteLine("Key = "+de.Key + ", Value = " + de.Value);
   }
}

Output

This will produce the following output −

Key/Value pairs...
Key = A, Value = AB
Key = B, Value = BC
Key = C, Value = DE
Key = D, Value = FG
Key = e, Value = fg

Example

Let us now see another example −

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      HybridDictionary myDict = new HybridDictionary(5, true);
      myDict.Add("A", "AB");
      myDict.Add("B", "BC");
      myDict.Add("C", "DE");
      myDict.Add("e", "fg");
      myDict.Add("E", "FG");
      Console.WriteLine("key/Value pairs...");
      foreach(DictionaryEntry de in myDict)
      Console.WriteLine("Key = "+de.Key + ", Value = " + de.Value);
   }
}

Output

This will produce the following error −

Run-time exception : An entry with the same key already exists.
Stack Trace:
[System.ArgumentException: An entry with the same key already exists.]
   at System.Collections.Specialized.ListDictionary.Add(Object key, Object value)
   at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value)
   at Demo.Main() :line 15

Updated on: 05-Dec-2019

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements