Force string interpolation to always follow CultureInfo.InvariantCulture
Asked Answered
T

1

10

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?

Thai answered 31/8, 2020 at 16:19 Comment(4)
You can specify if string interpolation should use the invariant culture by using learn.microsoft.com/en-us/dotnet/api/…Tommie
#2329797Ovolo
See #32077323Danonorwegian
This question is similar to: String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.Danonorwegian
M
12

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:

Milt answered 31/8, 2020 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.