String Interpolation in C# with Examples

String is one of the most used types in C# and it provides many options playing with it. I wrote a post earlier where I discussed about String, StringBuilder and String.Fromat. You might want to have a look here. Today I am going to one of the very nice features that got introduced with C# 6, named String Interpolation.

What is String Interpolation?

String Interpolation is a process of generating the output string from a string literal by replacing the placeholders with the actual values. These placeholders could be just variable names or some expressions. It is like template processing where the variables are replaced with actual values to generate the final output. We already have String.Fromat where the values are replaced based on the indexing of provided parameters but we don’t need that now. Interpolation is very common feature in many new libraries and frameworks like Angular.

We will see multiple examples based on various usage of string interpolation. Lets start with basic one

Example 1:

string name = "Brij";
string topic = "String Interpolation";

// Prior to C# 6
Console.WriteLine(string.Format("Hello {0}. Lets learn {1}",name, topic));

// C# 6 and later
Console.WriteLine($"Hello {name}. Lets learn {topic}");

Here both will give the same output. The second option is much cleaner and readable.  As in the above example, to use it, the string should start with $. Let’s see the syntax of interpolated string

$"<text> {<expression> [,<field-width>] [:<format-string>] } <text> ..."  
  • field-width – It defines the width of the field. If positive then value is right aligned and if negative, it is left aligned. If the value is less than the width of the value, there is no impact in the output.
  • format-string – It can be used to define the format of the value like datetime, currency, number etc.

Note – There should be no space between $ and ” at the start of the string else it will htow an compile time error.
Example 2:

int langversion = 6;
string VSVersion = "Visual Studio 2015";
DateTime releaseDate = new DateTime(2015, 7, 20);

Console.WriteLine($"C# {langversion: 0.0} was released with {VSVersion} on {releaseDate: MMM d yyyy}.");

// output

C#  6.0 got released with Visual Studio 2015 on  Jul 20 2015.

If we have any special characters that could provide different meaning to literal then it should be escaped like “, \ etc . If we want a curly braces ({ or }) in the string then it should be written twice as {{ or }}. To use few language elements like colon (:) in expression, it should be delimited by parenthesis as well in expression.

Example 3:

Console.WriteLine($"We can use special meaning characters like \", \\ by escaping it in literal text.");
Console.WriteLine($"We can use {{curly braces}} by using it twice.");
Console.WriteLine($"To use it in expression it should be delimited by paranthesis"
+$" like  2*2 = 4 is {(2*2 == 4? "Correct" : "Incorrect")}");

// Output
We can use special meaning characters like ", \ by escaping it in literal text.
We can use {curly braces} by using it twice.
To use it in expression it should be delimited by paranthesis like  2*2 = 4 is Correct

In the above example, we have seen that how to use special characters in string literals and in expression. Let’s see one more example

Example 4:

Console.WriteLine($"3 power 4 using Math function { Math.Pow(3, 4)} and using custom power "
$"function { CustomPow(3, 4)}.");

// Output
3 power 4 using Math function 81 and using custom power function 81.

Here we can see that we can use any framework class or even custom function in this expression.

Hope you have enjoyed this feature and will start using it.

Cheers,
Brij

Advertisements

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