June 29, 2017

Case Insensitive Dictionary


Very easy to implement in C#, just pass 'StringComparer.OrdinalIgnoreCase' in the constructor, for example:
Dictionary nameToIdTable = new Dictionary(StringComparer.OrdinalIgnoreCase);
Here is an an example in action:
[ Test ]
public void CaseInsenstiveDictionaryTest()
{
  var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase)
  {
    { "Mbukuravi", 1 },
    { "Liaedin", 2 },
  };

  Assert.That( dictionary[ "Liaedin" ] == 2 );
  Assert.That( dictionary[ "LiAEdin" ] == 2 );
  Assert.That( dictionary[ "LiaeDIn" ] == 2 );
  Assert.That( dictionary[ "LIAEDIN" ] == 2 );
}

No comments: