String Interpolation: A New Feature of C# 6.0

Introduction

On November 12, 2014, the day of the Visual Studio Connect() event, Microsoft announced Visual Studio 2015 preview with many new and exciting features for developers for testing purposes. Microsoft announced the new version of C#, C# 6.0, that came with many improvements and new features. One of the newly introduced features of C# 6.0 is String Interpolation.

What is String Interpolation

String Interpolation is another cool feature that helps you to manage string formatting easy. During the development period, formatting the string values are very common. While this is common, it may lead to errors because everytime we need to specify the placeholder for each item and then map them with the object values. String Interpolation lets you format strings in an easier manner that is regularly used to show the various records to the user. Earlier we either used the "+" symbol or the string.Format method to format and organize various strings that now became the old way to do string concatenation. String interpolation replaces the need for string.Format(...) by using escaped curly braces, \{...} , inside strings. With the release of C# 6.0 we can rewrite the strings and can put expressions directly in the string literal to show the values, we can also specify a space before and after the string and can also add conditions to the string.

Example 1

In the following code snippet we are using a string.Format() function that is a little complex, we need to put the numeric place holders {n} and based on the number we need to set the variables.

  1. var message = string.Format("Book Name :{0} Price :{1}", name, price);  

String Interpolation will help developers to use the actual variables as placeholders. Developers can directly use the actual variables as placeholders using String Interpolation, that can be looked at as an improvement. Therefore, the preceding code snippet can be re-written as:

  1. var message = "Book Name :\{name} Price :\{price}";  

Example 2

  1. public static void Main(string[] args)  
  2. {  
  3.    int a = 5, b = 10;  
  4.    Console.WriteLine(" The value A is \{a} while B is \{b}");  
  5.    Console.ReadKey();  
  6. }  
Example 3
  1. public static void Main(string[] args)  
  2. {  
  3.    string value1 = "Abhishek";  
  4.    string value2 = "Arora";  
  5.    string stringValue1 = string.Format("{0} {1}", value1, value2);     // With string.Format  
  6.    Console.WriteLine(stringValue1);  
  7.    string stringValue2 = "\{value1} \{value2}";      // With String Interpolation  
  8.    Console.WriteLine(stringValue2);  
  9. }  
Demo Application using Visual Studio 2013
  1. using System;  
  2. using System.Text;  
  3.   
  4. namespace CSharpFeatures  
  5. {  
  6.     class StringInterpolation  
  7.     {  
  8.         public string Name { getset; }  
  9.         public string Location { getset; }  
  10.         public string Age { getset; }  
  11.         public string Email { getset; }  
  12.         static void Main(string[] args)  
  13.         {  
  14.             StringInterpolation s = new StringInterpolation();  
  15.             s.Name = "Abhishek Arora";  
  16.             s.Location = "Ghaziabad";  
  17.             s.Age = "23";  
  18.             s.Email = "abhishek.arora22@gmail.com";  
  19.             Console.WriteLine("\n -- Old way of string concatenation using '+' Operator --");  
  20.             Console.WriteLine(" My Name is " + s.Name + ". I am from " + s.Location + ". I am " + s.Age + " years old. You can\n contact             me at " + s.Email);  
  21.             Console.WriteLine("\n\n -- Old way of string concatenation using String.Format Method --");  
  22.             Console.WriteLine(string.Format(" My Name is {0}. I am from {1}. I am {2} years old. You can\n contact me at {3} ", s.Name, s             .Location, s.Age, s.Email));  
  23.             Console.ReadKey();  
  24.         }  
  25.     }  
  26. }  

Demo Application using Visual Studio 2015 Preview

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace CSharpFeatures  
  8. {  
  9.     class StringInterpolation  
  10.     {  
  11.         public string Name { getset; }  
  12.         public string Location { getset; }  
  13.         public string Age { getset; }  
  14.         public string Email { getset; }  
  15.         static void Main(string[] args)  
  16.         {  
  17.   
  18.             StringInterpolation s = new StringInterpolation();  
  19.             s.Name = "Abhishek Arora";  
  20.             s.Location = "Ghaziabad";  
  21.             s.Age = "23";  
  22.             s.Email = "abhishek.arora22@gmail.om";  
  23.             Console.WriteLine("\n -- using String Interpolation Method with VS 2015 preview --");  
  24.             Console.WriteLine("\n My name is \{s.Name}. I am from \{s.Location}. I am \{s.Age} years old. You can\n contact me at \                   {s.Email}");  
  25.             Console.WriteLine("\n\n -- Condition Checking -- ");  
  26.             int totalamount = 2500;  
  27.             Console.WriteLine("\n Congrats \{s.Name} \{totalamount == 2500 ? "you are eligible for 10% discount!" : " No Discount"}");  
  28.             Console.ReadKey();  
  29.         }  
  30.     }  
  31. }  
 Summary

In this article we learned how to use String Interpolation without the use of string.Format. I hope you liked this new feature of C# 6.0 introduced by Microsoft. Don't forget to read my other articles on the series "A new feature of C# 6.0". Share your opinion about this feature and how will you use it in your project? Your comments are most welcome.