添加法语字符时 SortedList 抛出异常

use*_*957 5 c# special-characters sortedlist

我正在将一些独特的法语单词添加到排序列表中,但它似乎无法区分某些单词,例如“b\xc5\x93uf”和 boeuf”。

\n\n
private static void TestSortedList()\n{\n\n    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-fr");\n    SortedList sortedList = new SortedList(new Comparer(CultureInfo.CurrentCulture));\n\n    try\n    {\n        sortedList.Add("b\xc5\x93uf", "Value1");\n        sortedList.Add("boeuf", "Value1");\n    }\n    catch(Exception ex)\n    {\n        Console.WriteLine(ex.ToString());\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此上面的以下代码会抛出异常“System.ArgumentException:项目已被添加。”\n请帮忙!

\n

Zeb*_*ebi 2

    SortedList sortedList = new SortedList(StringComparer.Ordinal);\n\n    try\n    {\n        sortedList.Add("b\xc5\x93uf", "Value1");\n        sortedList.Add("boeuf", "Value1");\n    }\n    catch (Exception ex)\n    {\n        Console.WriteLine(ex.ToString());\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

作品。解释一下,OrdinalOrdinalIgnoreCase比较器比较字符字节,并且对于不同的字符它们是不同的。\n另请参阅 InvariantCulture 和 Ordinal 字符串比较之间的差异

\n