How to override case sensitivity in IDictionary <string, stringa>

I have vocabulary entries that are read on Dictionary <string,string> myDict=null;

The entries are as follows:

"user","Anthony"
"lastLogin","May 10 2010 20:43"

How do I get lastLoginwith a lowercase key myDict["lastlogin"]?

+5
source share
2 answers

The constructor for Dictionary<TKey,TValue>accepts a comparator object. You just need to pass in any mapper you want.

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

Of course, you can convey things like CurrentCultureIgnoreCaseor InvariantCultureIgnoreCase, depending on your needs.

+12
source

Pass IEqualityComparer<string>to the constructor Dictionary, for example StringComparer.CurrentCultureIgnoreCase.

0
source

All Articles