Subscribe to our YouTube Channel for more Videos

String Interpolation in C# 6.0

C# 6.0 Features Series

String Interpolation is yet another feature in C# 6.0 that lets the users to concatenate two or more strings together . The String Interpolation is not just about string concatenation. In the previous versions of C# , we used the + operator to concatenate strings or use String.Format method which provides additional options to format the string. The String.Format is one of the useful method and most frequently used method for formatting the strings but sometimes it can be error prone considering the usage of the arguments {1} or the placeholders as shown below.

var str = String.Format("{0} is a {1}", "Senthil Kumar", "Programmer");

With String Interpolation , the developers can put the expressions directly in the string literal and display the value in a properly formatted way. You can apply different format specifiers like the way you do it for string.format method as well as specify conditions within the string literals. Below is a sample code snippet demonstrating the usage of the String Interpolation in C# 6.0

string name = "senthil";
int age = 18;            
var input = "{name} is { age : D3} years old";
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.