案例敏感词典键 [英] Case Sensitive Dictionary Keys
问题描述
MyDict [a] == MyDict [A]
将返回true
我想知道的是为什么我得到一个已经添加的密钥错误我做
MyDict.Add(A,value1);
MyDict.Add(a,value2);
如果我定义了我的字典来区分大小写的查找。没有办法定义一个字典能够添加不同的套用键?
默认情况下,字典区分大小写 - 你不需要做任何事情。
字典< string,string> myDict = new Dictionary< string,string>();
myDict.Add(A,value1);
myDict.Add(a,value2);
查看您在线工作的代码: ideone 。
如果您的代码出现错误,那是因为这些密钥之一已经存在于您的字典中。
I've found plenty of info on the web about making dictionaries able to do case insensitive look-ups such that if I added a key/value pair of ("A", "value") calling
MyDict["a"] == MyDict["A"]
will return true.
What I want to know is why I get a "key has already been added" error when I do
MyDict.Add("A", "value1");
MyDict.Add("a", "value2");
if I defined my dictionary to do case sensitive look-ups. Is there no way to define a Dictionary to be able to add different cased keys?
Dictionaries are case-sensitive by default - you don't need to do anything.
Dictionary<string, string> myDict = new Dictionary<string, string>();
myDict.Add("A", "value1");
myDict.Add("a", "value2");
See your code working online here: ideone.
If you are getting an error with your code then it's because one of those keys already exist in your dictionary.
这篇关于案例敏感词典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!