TonyP
TonyP

Reputation: 5873

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

I have dictionary entries that are read to a Dictionary <string,string> myDict=null;

The entries are like:

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

How would I retrieve lastLogin with lowercase key myDict["lastlogin"] ?

Upvotes: 5

Views: 166

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456637

Pass an IEqualityComparer<string> into the Dictionary constructor, e.g., StringComparer.CurrentCultureIgnoreCase.

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422026

A constructor for Dictionary<TKey,TValue> takes a comparer object. You simply need to pass whatever comparer you want to it.

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

Of course, you may want to pass things like CurrentCultureIgnoreCase or InvariantCultureIgnoreCase depending on your need.

Upvotes: 12

Related Questions