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

172 Views Asked by At

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.

2

There are 2 best solutions below

0
Jon Skeet On BEST ANSWER

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.

0
manymanymore On

Another option, which I found out we are using at our project, is to have a separate country project which would have information about every country hardcoded. And after that any other project which needs this information can have a package reference to the country project.

This way we are getting a flexibility by being independent from the .NET RegionInfo. And in our country project the "CM" is a valid region name. We don't have to append neither the fr, not the en.