0

I'm working with a Dictionary in C# with .NET 3.5. I've created a Dictionary<string, int> object and passed in the 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?

aaronburro
  • 504
  • 6
  • 15
  • 3
    It evaluates to false on my machine. Could you double-check that you’re reading the correct value? – Douglas May 25 '12 at 21:18
  • 2
    looks like a PICNIC problem. mmmm It's actually a little confusing when you see "System.OrdinalComparer". makes you think that you really did supply StringComparer.Ordinal, because you don't see "IgnoreCase" anywhere... – aaronburro May 25 '12 at 21:21
  • @AustinSalonen `StringComparer.Ordinal` is independent of language. – Magnus May 25 '12 at 21:24

1 Answers1

7

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);
      }
    }
Magnus
  • 45,362
  • 8
  • 80
  • 118
agent-j
  • 27,335
  • 5
  • 52
  • 79
  • This is little difficult to override if we have to read a dictionary which is coming from a 3rd party library. – Kurkula Nov 17 '16 at 19:50