Search code examples
c#.netcultureinfo

Force string interpolation to always follow CultureInfo.InvariantCulture


For a given .NET assembly compiled from C# (latest version), I would like to force all the string interpolations to systematically use CultureInfo.InvariantCulture instead of using CultureInfo.CurrentCulture. Is there any way to enforce this?


Solution

  • You can use the FormattableString.Invariant(FormattableString) Method.

    Place a

    using static System.FormattableString; 
    

    at the top of your code and then use like

    string interpolated = Invariant($"My interpolated string {3.5:N2}");
    

    For background information look at: