案例敏感词典键 [英] Case Sensitive Dictionary Keys

查看:116
本文介绍了案例敏感词典键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网络上找到了很多关于使字典能够区分大小写的查询的信息,以便如果我添加了一个键值/值对(A,值),调用

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆