Question

I am adding some unique french words to a sorted list, but it doesn't seem to differentiate certain words like "bœuf" & 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 throws exception "System.ArgumentException: Item has already been added." Please help!

Was it helpful?

Solution

    SortedList sortedList = new SortedList(StringComparer.Ordinal);

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

works. To explain, the Ordinal and OrdinalIgnoreCase comparers compare the characters bytes and they are different for different chars. See Difference between InvariantCulture and Ordinal string comparison too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top