C# 6 – String Interpolation

The – in my opinion – best two C# 6 features are the null propagation operator and string interpolatation. In case you are still not using these features, here is a short introduction to string interpolation.
Interpolation

German Version

String Interpolation

C# 6 introduces string interpolation by using the $ prefix. Instead of writing this:

string s1 = "World";
string s2 = string.Format("Hello, {0}!", s1);

you can simply do this instead:

string s1 = "World";
string s2 = $"Hello, {s1}!";

String interpolation reduces code and makes it easier to read!

The compiler generates the same code, or can even optimize depending on the formats you apply. You can even add format strings as you are used to.

Assigning the interpolated string to a FormattableString variable, you can access the holes in the string and can do other interesting features to change the string:

int x = 3, y = 4;
FormattableString s = $"The result of {x} + {y} is {x + y}";
WriteLine($"format: {s.Format}");
for (int i = 0; i < s.ArgumentCount; i++)
{
  WriteLine($"argument {i}: {s.GetArgument(i)}");
}

results in this output:

format: The result of {0} + {1} is {2}
argument 0: 3
argument 1: 4
argument 2: 7

FormattableString allows custom feature extensions with string interpolation

Using the FormattableString as an argument, you can easily change the string result, e.g. by using an invariant culture:

string Invariant(FormattableString s) =&gt;
  s.ToString(CultureInfo.InvariantCulture);

Now creating a DateTime instance, the date can be written in the default format, as well as an invariant format:

var day = new DateTime(2025, 2, 14);
WriteLine($"{day:d}");
WriteLine(Invariant($"{day:d}");

The FormattableString class already defines the static method Invariant that does the same:

WriteLine(FormattableString.Invariant($&quot;{day:d}&quot;);

There’s More!

Other features of C# 6, such as the wonderful null propagation operator follows in another blog post. String interpolation as well as null propagation, static using, expression-bodied methods, expression-bodied properties, auto implemented property initializers, read-only auto properties, the nameof operator, dictionary intitializers, exception filters, await in catch and more are covered in my new book Professional C# 6 and .NET Core 1.0. String interpolation is explained in detail in Chapter 10, “Strings and Regular Expressions”, and used in samples throughout the book. You can also get more information about the C# 6 features in my .NET workshops.

What are the C# 6 features you like the most?

Christian

5 thoughts on “C# 6 – String Interpolation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s