c# – 添加法语字符时抛出异常的SortedList
作者:互联网
我在排序列表中添加了一些独特的法语单词,但它似乎没有区分某些单词,如“bœuf”&伯夫”.
private static void TestSortedList()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-fr");
SortedList sortedList = new SortedList(new Comparer(CultureInfo.CurrentCulture));
try
{
sortedList.Add("bœuf", "Value1");
sortedList.Add("boeuf", "Value1");
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
因此,上面的代码抛出异常“System.ArgumentException:Item已被添加.”
请帮忙!
解决方法:
SortedList sortedList = new SortedList(StringComparer.Ordinal);
try
{
sortedList.Add("bœuf", "Value1");
sortedList.Add("boeuf", "Value1");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
作品.为了解释,Ordinal和OrdinalIgnoreCase比较器比较字符字节,它们对于不同的字符是不同的.
也见Difference between InvariantCulture and Ordinal string comparison.
标签:c,special-characters,sortedlist 来源: https://codeday.me/bug/20190708/1406793.html