List sort exception when adding french characters

I add some unique French words to the sorted list, but it doesn't seem to highlight some words like "bœuf" and boeuf ".

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());
    }
}

So, the following code above raises the exception "System.ArgumentException: Item has already been added." Please, help!

+4
source share
1 answer
    SortedList sortedList = new SortedList(StringComparer.Ordinal);

    try
    {
        sortedList.Add("bœuf", "Value1");
        sortedList.Add("boeuf", "Value1");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

working. To explain comparisons Ordinaland OrdinalIgnoreCasecompare character bytes, and they are different for different characters. See The difference between InvariantCulture and string string comparisons .

+1
source

All Articles