String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

前端 未结 4 1297
陌清茗
陌清茗 2021-02-02 08:21

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 brea

4条回答
  •  滥情空心
    2021-02-02 09:03

    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}");
    

提交回复
热议问题