The Dictionary class has several overloaded constructors. Here we use one that accepts a generic IEqualityComparer parameter.
using System;
using System.Collections.Generic;
// Create case insensitive string Dictionary.
var test = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
test.Add(
"Cat", 2);
test.Add(
"Python", 4);
test.Add(
"DOG", 6);
// Write value of "cat".
Console.WriteLine(
"{0} = {1}", test.GetValueOrDefault(
"cat"), test.GetValueOrDefault(
"CAT"));
// Write value of "PYTHON".
Console.WriteLine(
"{0} = {1}", test.GetValueOrDefault(
"PYTHON"), test.GetValueOrDefault(
"Python"));
// See if "dog" exists.
if (test.ContainsKey(
"dog"))
{
Console.WriteLine(
"Contains dog.");
}
// Enumerate all KeyValuePairs.
foreach (var pair in test)
{
Console.WriteLine(pair);
}
2 = 2
4 = 4
Contains dog.
[Cat, 2]
[Python, 4]
[DOG, 6]