Keywords

I found a lot of information on the Internet about how to make dictionaries available for case-insensitive viewing in such a way that if I added a key / value pair ("A", "value") that calls

MyDict["a"] == MyDict["A"] 

will return true.

What I want to know is that I get a "key already added" message when I do

 MyDict.Add("A", "value1"); MyDict.Add("a", "value2"); 

if I defined my dictionary to make case-sensitive queries. Is it impossible to define a dictionary in order to be able to add different overlaid keys?

+6
source share
3 answers

Dictionaries are case sensitive by default - you don't have to do anything.

 Dictionary<string, string> myDict = new Dictionary<string, string>(); myDict.Add("A", "value1"); myDict.Add("a", "value2"); 

See your code here: ideone .

If you get an error message with your code, then this is because one of these keys already exists in your dictionary.

+9
source

All Dictionaries case sensitive. But you can use a case-insensitive string comparisons, provided by the class StringComparer , to create dictionaries with case-insensitive string keys.

Give up ideone .

+6
source

OP is really right if he used the StringDictionary class. The Microsoft website claims that the key is converted to lowercase before saving it ( https://msdn.microsoft.com/en-us/library/system.collections.specialized.stringdictionary(v=vs.110).aspx ). To make key case sensitive, using the Generic Dictionary, as Mark Byers suggests, works great. If you want a case-insensitive key, StringDictionary works well.

+1
source

All Articles