String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

.net, c#, string.format, vb.net, visual-studio-2015

Solution

Microsoft has made it easier to use string interpolation and comply with CA1305: Specify IFormatProvider.

If you are using C# 6 or later, you have access to the `using static` directive.

In addition, the static method `FormattableString.Invariant` is available for .NET Standard 1.3, .NET Core 1.0 and .NET Framework 4.6 and later. Putting the two together allows you to do this:

using static System.FormattableString;

string name = Invariant($"Hello {name}");

If, however, your goal is for the interpolation to be done via the current culture, then a companion static method `FormattableString.CurrentCulture` is proposed in .NET Core 3.0 (currently, Preview 5):

using static System.FormattableString;

string name = CurrentCulture($"Hello {name}");

Problem

The new string interpolation style in Visual Studio 2015 is this: ``` Dim s = $"Hello {name}" ``` But if I use this the code analysis tells me I break CA1305: Specify IFormatProvider In the old times I did it like this: ``` Dim s = String.Format(Globalization.CultureInfo.InvariantCulture, "Hello {0}", name) ``` But how can it be done with the new style? I have to mention that I'm looking for a solution for .Net 4.5.2 (for .Net 4.6 dcastro has the answer)

Original source