I was trying to use SortedDictionary to store some of my data from file, but got really weird bunch of key duplication exceptions. I came up with the next code sample which reproduces my issue:
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.
It's not happening for usual Dictionary though and I finally decided to use it instead. But I still wonder why is it a problem for a sorted one? Unfortunatelly, I was unable to google an answer myself (got a lot of AES-related noise) and can't debug into SortedDictionary's code despite MS recently made some of .NET source code open.
It's almost seems like this class implicitly runs some string preprocessing/normalization function, but I just can't believe it is an actual reason.
Any ideas why is it happening? Thanks in advance!
It's because of the culture. Try new SortedDictionary(StringComparer.Ordinal)
, for example.
The reason Dictionary behaves differently is that it uses EqualityComparer<TKey>.Default
while SortedDictionary uses Comparer<TKey>.Default
.