Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between String, FormattableString, IFormattable

FormattableString has been Introduced in C# 6.0. As we can use same string formatting using string object why is there need of using FormattableString or IFormattable. Whats difference between three?

My Code

        var name = "Pravin";         var s = $"Hello, {name}";         System.IFormattable s1 = $"Hello, {name}";         System.FormattableString s2 = $"Hello, {name}"; 

Above all there produces same result. i.e 'Hello Pravin'.

Can I get more elaborated answer on this if anyone has deep knowledge on same.

like image 977
Nitin Varpe Avatar asked Feb 16 '16 07:02

Nitin Varpe


People also ask

What does string interpolation do?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

What is Formattable string in C#?

IFormattable.ToString(String, IFormatProvider) Returns the string that results from formatting the format string along with its arguments by using the formatting conventions of a specified culture.

What is the correct syntax for string interpolation?

Syntax of string interpolation starts with a '$' symbol and expressions are defined within a bracket {} using the following syntax. Where: interpolatedExpression - The expression that produces a result to be formatted.

What is in front of string?

What is @ in front of a string in C#? It marks the string as a verbatim string literal. In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier.


1 Answers

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 
like image 104
Jon Skeet Avatar answered Sep 17 '22 16:09

Jon Skeet