Problem with SortedDictionary in C# 11 with .NET 4.8 / Globalization

A web site uses the current culture of the user for displaying data. A problem occured when a user from Vietnam accessed the data. Can someone explain why this problems occurs?
Code to reproduce the error:

static void SortedDicProblem(Action<string> Write)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

    const string key = "KH";

    SortedDictionary<string, string> laCountries = new()
    {
        { key, "Kambodscha " },
        { "KI", "Kiribati " },
        { "KM", "Komoren " },
    };

    foreach (CultureInfo loCult in System.Globalization.CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures))
    {
        Test(loCult.Name);
    }

    void Test(string vsCulture)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(vsCulture);

        if (laCountries.TryGetValue(key, out string lsName))
        {
            Write($"{vsCulture}");
        }
        else
        {
            Write($"<span class='error'>{vsCulture}, Key not found: {key}, {Thread.CurrentThread.CurrentCulture.EnglishName}</span>");
        }
    }
}

We’ll know how to solve it, but what’s the reason for this error?
A Solution to avoid the problem: Instantiate the SortedDictionary with a StringComparer, like:

new SortedDictionary<string,string>(StringComparer.Ordinal)

5

Here is the issue (occurs on .NET 4.8, not on .NET 5+):

public static void Main()
{
    var usComparer = StringComparer.Create(new CultureInfo("en-US"), ignoreCase: false);
    var veComparer = StringComparer.Create(new CultureInfo("vi-VN"), ignoreCase: false);

    Console.WriteLine(usComparer.Compare("KH", "KM")); // Prints -1
    Console.WriteLine(veComparer.Compare("KH", "KM")); // Prints  1
}

In English-US, “KH” is considered to be before “KM”.
In Vietnamese, “KH” is considered to be after “KM”.

Because you have inserted the items into the dictionary using one comparer, and then tried to retrieve them using a different comparer which gives a different sort order, it all falls apart.

As to why it’s different on .NET 5+ – well that’s a different question. I would imagine that it’s to do with the fact that .NET core uses ICU for string comparisons and .NET 4.8 uses NLS.


You can make the issue also occur on .NET 5+ by forcing the use of NLS rather than ICU by adding the following to the project file and recompiling:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
</ItemGroup>

ADDENDUM

I couldn’t work out why this was failing, given that the implementation of SortedDictionary caches the comparer. Surely it should give the same results even when you change the locale?

Well it turns out that Comparer<string>.Default actually changes its behaviour when you change locale, even if it’s been cached!

Consider this code, which demonstrates:

var comparer = Comparer<string>.Default; // "Cache" the comparer.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine(comparer.Compare("KH", "KM"));                  // Prints -1
Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
Console.WriteLine(comparer.Compare("KH", "KM"));                  // Prints 1

That explains why the search goes wrong even though the comparer was cached.

But what explains the behaviour of the cached comparer? It’s quite simple. The cached comparer is of type GenericComparer<T>, the implementation of which is:

    public override int Compare(T x, T y)
    {
        if (x != null)
        {
            if (y != null) return x.CompareTo(y);
            return 1;
        }
        if (y != null) return -1;
        return 0;
    }

So since T is string, this is effectively just calling the string.CompareTo() method, which will be using the current locale.

11

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật