SortedDictionary ArgumentException: "Æ" and "AE" are considered the same keys

I tried using SortedDictionary to store some of my data from a file, but got a really strange bunch of key duplication exceptions. I came up with the following code example that reproduces my problem:

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.

This code in .NET Fiddle

This does not happen for a regular dictionary, although I finally decided to use it. But I still wonder why this is a problem for sorting? Unfortunately, I couldn’t answer the question myself (I got a lot of noise related to AES), and I can’t debug the SortedDictionary code, despite the fact that MS recently opened some kind of .NET source code.

This class seems to implicitly run some string preprocessing / normalization function, but I just can't believe this is the real reason.

Any ideas why this is happening? Thanks in advance!

+4
source share
1 answer

This is because of the culture. For example, try new SortedDictionary(StringComparer.Ordinal).

The reason the Dictionary behaves differently is because it uses EqualityComparer<TKey>.Default, while SortedDictionary uses Comparer<TKey>.Default.

+5
source

All Articles