1

I need to determine whether a Dictionary I receive in a function has a case-insensitive comparer.

ie. A case insensitive dictionary is declared like so:

var myDict = new Dictionary<string, decimal>(StringComparer.OrdinalIgnoreCase);

When I receive the dictionary into my function I can access the .Comparer property and check its type - however all I seem to get is -

System.Collections.Generic.EqualityComparer<string> 

How can I find out if the Comparer being used is StringComparer.OrdinalIgnoreCase?

dan
  • 5,664
  • 8
  • 45
  • 59
  • 2
    I cannot reproduce your scenario; passing that comparer in and checking the `.Comparer` gets a `System.OrdinalComparer`. Are you sure you aren't using the default constructor, i.e. `new Dictionary()` ? – Marc Gravell Sep 26 '12 at 06:36
  • hmm - I've stepped through the code and I'm fairly certain I'm not using the default constructor. The other thing is that the behaviour of the function changes with respect to the case-sensitivity - so the StringComparer.OrdinalIgnoreCase is having an effect when I add it to the constructor. – dan Sep 26 '12 at 06:43
  • Sorry - you're right. I was getting mixed up between some test code and the real stuff. The answer below does the trick. – dan Sep 26 '12 at 06:47

1 Answers1

3

You should check the value, not the type against StringComparer.OrdinalIgnoreCase.

myDict.Comparer == StringComparer.OrdinalIgnoreCase
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72