Reputation: 554
I'm working with a Dictionary in C# with .NET 3.5. I've created a Dictionary
object and passed in the <string, int
>StringComparer.Ordinal
equality comparer. When I do the following code, however, I don't get what I would expect:
Dictionary<string, int> theDictionary = new Dictionary<string, int>(StringComparer.Ordinal);
theDictionary.Add("First", 1);
bool exists = theDictionary.ContainsKey("FIRST"); // equals true, when it should not
What am I not seeing here?
Upvotes: 0
Views: 3019
Reputation: 27943
Are you sure you didn't use StringComparer.OrdinalIgnoreCase?
This code prints false for me with C# v3.5 compiler:
using System;
using System.Collections.Generic;
static class Program
{
static void Main()
{
Dictionary<string, int> theDictionary = new Dictionary<string, int>(StringComparer.Ordinal);
theDictionary.Add("First", 1);
bool exists = theDictionary.ContainsKey("FIRST");
Console.WriteLine(exists);
}
}
Upvotes: 7