String Interpolation In C# 6.0

In this article, you will learn:

  • String concatenation in C# (Old mechanism vs New mechanism)
  • What is String Interpolation in C#?
  • How does it work?

What is String Interpolation in C#

String Interpolation is a mechanism to concatenate two or more strings together.

In older versions of C#, we were using “+” operator or string. Format method was used to concatenate strings, but in C# 6.0, Microsoft has provided a feature named String Interpolation to concatenate strings.

Let’s see an example of Customer class where we will concatenate the first name and last name using traditional ways of concatenation.

Traditional ways of String Concatenation:

Using “+” Operator

  1. class Customer  
  2. {  
  3.     public string FirstName  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string LastName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Email  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public int age  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     static void Main(string[] args)  
  24.     {  
  25.         // In Visual Studio 2013   
  26.         Customer cus = new Customer();  
  27.         cus.FirstName = "Lana";  
  28.         cus.LastName = "Leonard";  
  29.         cus.Email = "[email protected]";  
  30.         cus.age = 31;  
  31.         Console.WriteLine("Name : " + cus.FirstName + " " + cus.LastName + "\nEmail : " + cus.Email);  
  32.         Console.ReadLine();  
  33.     }  
  34. }  
Using String.Format method
  1. class Customer  
  2. {  
  3.     public string FirstName  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string LastName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Email  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public int age  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     static void Main(string[] args)  
  24.     {  
  25.         // In Visual Studio 2013   
  26.         Customer cus = new Customer();  
  27.         cus.FirstName = "Lana";  
  28.         cus.LastName = "Leonard";  
  29.         cus.Email = "[email protected]";  
  30.         cus.age = 31;  
  31.         Console.WriteLine(string.Format("Name : {0} {1}\nEmail : {2}", cus.FirstName, cus.LastName, cus.Email));  
  32.         Console.ReadLine();  
  33.     }  
  34. }  
New ways of String Concatenation

Using String Interpolation:

By using the new String Interpolation feature in C# 6.0, you can:
  • Place the String whereever you want it.
  • Use conditions.
  • Specify space before / after the string.

So, let’s see an example to implement the above 3 scenarios using String Interpolation.

  1. class Customer  
  2. {  
  3.     public string FirstName  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string LastName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Email  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public int age  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     static void Main(string[] args)  
  24.     {  
  25.         // In Visual Studio 2015   
  26.         Customer cus = new Customer();  
  27.         cus.FirstName = "Lana";  
  28.         cus.LastName = "Leonard";  
  29.         cus.Email = "[email protected]";  
  30.         cus.age = 31;  
  31.         // Putting String Where you need   
  32.         Console.WriteLine("Name : \{cus.FirstName} \{cus.LastName}\nEmail : \{cus.Email}\nAge :\{cus.Age}");  
  33.         // Putting Space After FirstName (5 pixel)  
  34.         Console.WriteLine("Name : \{cus.FirstName,5} \{cus.LastName}\nAge :\{cus.Age:A2}");  
  35.         //Putting Condition in string literal, If Age>=30 it will display Message: Senior Developer else Junior Developer  
  36.         Console.WriteLine("Name : \{cus.FirstName, 5} \{cus.LastName}\nMessage :\{cus.Age>=30?"  
  37.             " :"  
  38.             Senior Developer "}");  
  39.         Console.ReadLine();  
  40.     }  
  41. }  


Recommended Free Ebook
Similar Articles