Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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"] ?

like image 979
TonyP Avatar asked May 20 '10 12:05

TonyP


People also ask

Is Containskey case-sensitive C#?

The key is handled in a case-insensitive manner; it is translated to lowercase before it is used.

Are Keys in dictionaries case-sensitive?

All Dictionaries are case-sensisitive.

Is key value case-sensitive?

User metadata keys are case insensitive and are returned as lowercase strings, even if they were originally specified with uppercase strings.

Are dictionaries in Python case-sensitive?

A Python dictionary sub-class that is case-insensitive when searching, but also preserves the keys as inserted. when keys are listed, ie, via keys() or items() methods. pair as the key's value (values become dictionaries).


1 Answers

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.

like image 79
mmx Avatar answered Sep 28 '22 22:09

mmx