Difference between CultureInfo.Get and new CultureInfo

 
 
  • Gérald Barré

To get a CultureInfo object in .NET, you can use either the static method CultureInfo.GetCultureInfo or the class constructor. This post explains the difference between the two approaches.

C#
var cultureInfo1 = CultureInfo.GetCultureInfo("en-US");
var cultureInfo2 = new CultureInfo("en-US");

The constructor creates a new, mutable instance for the specified culture. You can modify its properties, such as the calendar and number format. The following example changes the currency symbol:

C#
var culture = new CultureInfo("en-US");
culture.NumberFormat.CurrencySymbol = "€";

Console.WriteLine(42.ToString("C", culture)); // €42.00

The static method CultureInfo.GetCultureInfo returns a read-only instance of the specified culture. Because the instance is immutable, the method can cache it, making calls faster and reducing memory allocations.

C#
var culture1 = CultureInfo.GetCultureInfo("en-US");
var culture2 = CultureInfo.GetCultureInfo("en-US");
Console.WriteLine(ReferenceEquals(culture1, culture2)); // True

Running a simple benchmark with BenchmarkDotNet shows the allocation difference between the two methods and confirms how caching reduces memory usage.

C#
[MemoryDiagnoser]
public class CultureInfoBenchmark
{
    [Benchmark(Baseline = true)]
    public void Ctor()
    {
        _ = new CultureInfo("en-US");
    }

    [Benchmark]
    public void StaticMethod()
    {
        _ = CultureInfo.GetCultureInfo("en-US");
    }
}
MethodMeanAllocated
Ctor33.74 ns144 B
StaticMethod24.44 ns32 B

Use CultureInfo.GetCultureInfo in most cases, since directly modifying a CultureInfo instance is uncommon.

#Additional resources

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?