tags:

views:

204

answers:

2

I have a Dictionary called AvailableBoxes. I want to lookup a string in this dictionary. However, I want to compare lowercase (and ignore special chars).

I found out that I can do the following to know if the key exists in the dictionary:

AvailableBoxes.Keys.Contains(CurrentBox.Name, new BoxNameEqualityComparer())

BoxNameEqualityComparer is my own comparer that implements IEqualityComparer.

But is there a similar way to find the index of this key?

+1  A: 

Look at using one of the predefined ones in the StringComparer class.

leppie
+1  A: 

If you are just comparing strings of no case, there is no need to define your own comparer. Simply use the built-in StringComparer class

AvailableBoxes.Keys.Contains(CUrrentBox.Name, StringComparer.OrdinalIgnoreCase);

It is possible to determine an index of a particular key if you view the Keys collection as an ordered list. That is a bad assumption though. Because if you add a new item into the dictionary it can appear at any place in the Keys collection.

If you truly want ordering in your Dictionary, I suggest you use OrderedDictionary.

JaredPar