Difference between String, FormattableString, IFormattable

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 19:58:01

FormattableString is a new type in .NET 4.6, and the compiler will only use it if you try to use it. In other words, the type of an interpolated string literal is normally string - built with string.Format - but can be FormattableString (via FormattableStringFactory) if you ask for it.

A FormattableString consists of the format string which would be passed to string.Format (e.g. "Hello, {0}") and the arguments that would be passed in order to format it. Crucially, this information is captured before formatting.

This allows you to adjust the formatting appropriately - most commonly to perform it in the invariant culture, often with the Invariant static method.

When you assign an interpolated string literal to an IFormattable variable, that will use FormattableString too. The IFormattable.ToString(string, CultureInfo) implementation ignores the first argument in this case, which is presumably why it uses explicit interface implementation.

Sample code:

using System;
using System.Globalization;
using System.Threading;
using static System.FormattableString;

class Test
{
    static void Main()
    {
        var uk = CultureInfo.CreateSpecificCulture("en-GB");
        Thread.CurrentThread.CurrentCulture = uk;
        var germany = CultureInfo.CreateSpecificCulture("de-DE");
        string now = $"Default: it is now {DateTime.UtcNow}";
        Console.WriteLine(now); // UK format
        IFormattable x = $"Specific: It is now {DateTime.UtcNow}";
        Console.WriteLine(x.ToString("ignored", germany));
        FormattableString y = $"FormattableString: It is now {DateTime.UtcNow}";
        Console.WriteLine(FormattableString.Invariant(y));
        // Via using static
        Console.WriteLine(Invariant($"It is now {DateTime.UtcNow}")); 
    }
}

Sample results:

Default: it is now 16/02/2016 07:16:21
Specific: It is now 16.02.2016 07:16:21
FormattableString: It is now 02/16/2016 07:16:21
It is now 02/16/2016 07:16:21
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!