开发者

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

I have dictionary entries that are read to a Diction开发者_高级运维ary <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"] ?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜