String Interpolation ($) feature introduced in C# 6.0, allows embedded expression in a string . The syntax prefixes a string literal with a dollar $ operator symbol and then embeds the expressions with curly braces ({}).

Lets look at an example below,

In the above example, we have used the $ operator and variable expressions as firstName and lastName . When we run above sample then it prints “Full Name : Raul Martinez” .

String Interpolation & String.Format() :

The $ operator in C# or String Interpolation is an extended version of the String.Format() feature . However, it allows a more readable and convenient way of combining formatted string expression results into a string literal.

We can rewrite above string interpolation example by using String.Format() method as follows.

While using String.Format() , here we insert the variable values in a string using index numbers like {0}{1}, etc.

However, string interpolation allow us to directly include valid C# expression in side a string literal using braces like {expression}.  Console.WriteLine($"Full Name : {firstName} {lastName}")

Formatting using String Interpolation :

We can also format the object using control formatting syntax using string interpolation expression. It uses below syntax for formatting an object.

${variable expression : formatting expression}

  • variable expression :  The string variable expression.
  • formatting expression : It is the formatting expression of the result string output.

For example, in below code we have applied formatting information as {enrollmentDate:D} for the enrollmentDate variable.

Alignment using String Interpolation :

Along with formatting the object value, we can also do alignment and add spacing to it. You can define the width of the string that is inserted into the result string by using syntax such as {enrollmentDate, 10} , it will inserts 10-character string in to the result string value.

It uses below syntax for adding spaces to object.

${variable expression , alignment value}

  • alignment value : A signed integer value, that defines the width of string that is going to be inserted to the result string.
    • To left-align strings in a field, you have to provide negative value, such as {enrollmentDate, -10} 
    • To right-align string in a field, you have to provide a positive value, such as {enrollmentDate, 10}

We can also include formatting string along with the alignment value to the expression as follows.

${variable expression , alignment value : formatting expression} 

Lets look at below example to understand how this alignment and formatting expression works.

When we run above program then it generates below output.

string interpolation output 1.0

If you look at the output, then you can find that we have implemented.

  • Left alignment by using $”{“Date”, -20} {“Temperature”, -15} for both first and second argument.
  • Right alignment by using $”{“Date”, 20} { “Temperature”, 15} for both arguments.
  • We have used the formatting expression $”{tempDate,-20 : g} {temp, -15 : N1}” and $”{tempDate,20 : g} {temp, 15 : N1} to format the result string.