【发布时间】:2015-05-27 08:41:13
【问题描述】:
我试图使用 SortedDictionary 从文件中存储我的一些数据,但得到了非常奇怪的一堆关键重复异常。我想出了下一个重现我的问题的代码示例:
var dict = new SortedDictionary<string, string>();
dict.Add("Æ", "qwerty"); // "aesc" (aka "ash"), single symbol
Console.WriteLine(dict["AE"]); // outputs "qwerty" for two-symbol string "AE"
dict.Add("AE", ""); // ArgumentException: An entry with the same key already exists.
不过,通常的 Dictionary 并没有发生这种情况,我最终决定改用它。但我仍然想知道为什么排序的问题是个问题?不幸的是,尽管 MS 最近开放了一些 .NET 源代码,但我自己无法通过谷歌搜索答案(有很多与 AES 相关的噪音)并且无法调试到 SortedDictionary 的代码。
这个类似乎隐式运行了一些字符串预处理/规范化函数,但我不敢相信这是一个实际原因。
任何想法为什么会发生?提前致谢!
【问题讨论】:
-
因为
string.Compare("Æ", "AE") == 0。查看备注here。
标签: c# .net dictionary unique sorteddictionary