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 responses to “C# 6 – String Interpolation”

  1. […] a previous article I explained the C# 6 string interpolation which is really a great enhancement with C# 6. With this follow-on article I’m showing […]

    Like

  2. […] programmers as well. You can read about new C# 6 features such as the null-conditional operator, string interpolation, expression bodied members, read-only auto properties, the nameof operator, and more. You can even […]

    Like

  3. […] Web applications. You can read about new C# 6 features such as the null-conditional operator, string interpolation, expression bodied members, read-only auto properties, the nameof operator, and […]

    Like

  4. […] UWP applications. You can read about new C# 6 features such as the null-conditional operator, string interpolation, expression bodied members, read-only auto properties, the nameof operator, and […]

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About the Podcast

Welcome to Christian Nagel’s Blog by CN innovation — a place for insights, tutorials, and real-world experience in modern software development. Christian Nagel and the CN innovation team share practical knowledge about .NET, C#, Azure, ASP.NET Core, WinUI, .NET MAUI, cloud architecture, and current technology trends.