Search code examples
c#.net-6.0isocultureregion

How could I use the "CM" region in .NET 6?


I have a .NET 6 application. I am trying to run this code:

using System;
using System.Text;
using System.Globalization;
                    
public class Program
{
    public static void Main()
    {
        new RegionInfo("CM");
    }
}

I am getting this error:

Unhandled exception. System.ArgumentException: The region name CM should not correspond to neutral culture; a specific culture name is required. (Parameter 'name') at System.Globalization.RegionInfo..ctor(String name) at Program.Main() Command terminated by signal 6

Here is a fiddle.

The same code works fine in .NET 4.7.2. Also, I checked here that the "CM" is a valid ISO 3166 country code for Cameroon.

So, is this behavior a C# bug? I have an application where users can add countries. Now my application fails when the Cameroon is added. How could I fix or go around it?

It is a big application, so I can not just downgrade it to the .NET 4.7.2.


Solution

  • The error message isn't saying that CM isn't a valid culture name - just that it's a neutral culture rather than a specific culture.

    Apparently you need to specify a language/region combination, e.g. "fr-CM" for French (Cameroon) or "en-CM" for English (Cameroon).

    I honestly don't know why the behavior is different in .NET Framework to .NET 6, but the above appears to at least allow the constructor call to complete. Of course, you'll need to check what the impact on the rest of the application is.