C# | Creating an empty HybridDictionary with specified case sensitivity
Last Updated :
01 Feb, 2019
HybridDictionary(Boolean) constructor creates an empty HybridDictionary with the specified case sensitivity.
Syntax:
public HybridDictionary (bool caseInsensitive);
Here, caseInsensitive is a Boolean that denotes whether the HybridDictionary is case-insensitive.
Below given are some examples to understand the implementation in a better way:
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary( false );
myDict.Add( "I" , "first" );
myDict.Add( "i" , "first" );
myDict.Add( "II" , "second" );
myDict.Add( "III" , "third" );
myDict.Add( "IV" , "fourth" );
myDict.Add( "V" , "fifth" );
foreach (DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " " + de.Value);
}
}
|
Output:
I first
i first
II second
III third
IV fourth
V fifth
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary( true );
myDict.Add( "A" , "Apple" );
myDict.Add( "a" , "Air" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "d" , "Dolphine" );
myDict.Add( "E" , "Elephant" );
myDict.Add( "F" , "Fish" );
foreach (DictionaryEntry de in myDict)
Console.WriteLine(de.Key + " " + de.Value);
}
}
|
Runtime Error:
Unhandled Exception:
System.ArgumentException: An entry with the same key already exists.
at System.Collections.Specialized.ListDictionary.Add
Note: This constructor is an O(1) operation.
Share your thoughts in the comments
Please Login to comment...