String Interpolation In C# 6.0

In this article we will learn how to concatenate/interpolate strings with C# 6.0.

Introduction

 
This article explains String Interpolation. As we know Microsoft has announced many new features with Visual Studio 2015 Preview. It was announced on November 12, 2014 on the day of the Visual Studio Connect() event from New York, USA.

Visual C# 6.0, .NET Framework 4.6 and Roslyn Compiler has come with Visual Studio 2015 Preview. We will now explore String Interpolation. First we should understand what String Interpolation means.

CSharp6.0
Figure 1.0

String Interpolation is nothing but a way to concatenate two or more strings together. As we aware of, in previous version of .NET we did string concatenation using  the + (plus) operator. Sometimes for the same work we have also used the String.Format method. Now we are pleased to hear that it became the old way to do string concatenation.

Before C# 6.0 

Before discussing String Interpolation/concatenation with C# 6.0, let's discuss the old way to concatenate the string. As we know we have two ways  to implement string interpolation with the previous version, they are: 

  • C# 1.0
  • C# 2.0
  • C# 3.0
  • C# 4.0 and
  • C# 5.0 as well

Example

Old way to concatenate strings using the + operator.

See the following program written in Visual Studio 2013. It will run on any lower version of C# 6.0. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace StringInterpolation  
  7. {  
  8.     class StringInterpolation  
  9.     {  
  10.          public string FirstName { getset; }  
  11.          public string LastName { getset; }  
  12.          public string Email { getset; }  
  13.   
  14.          static void Main(string[] args)  
  15.          {  
  16.              // In Visual Studio 2013  
  17.   
  18.              StringInterpolation si = new StringInterpolation();  
  19.              si.FirstName = "Praveen";  
  20.              si.LastName = "Kumar";  
  21.              si.Email = "kumar.praveen@yahoo.com";  
  22.   
  23.              Console.WriteLine("Name : "+ si.FirstName+" "+si.LastName+"\nEmail : "+si.Email);  
  24.              Console.ReadLine();    
  25.          }  
  26.     }  
  27. }  

OldWay1
Figure 1.1

Example

Old way to concatenate strings using a placeholder. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace StringInterpolation  
  7. {  
  8.     class StringInterpolation  
  9.     {  
  10.        public string FirstName { getset; }  
  11.        public string LastName { getset; }  
  12.        public string Email { getset; }  
  13.   
  14.        static void Main(string[] args)  
  15.        {  
  16.            // In Visual Studio 2013  
  17.   
  18.             StringInterpolation si = new StringInterpolation();  
  19.             si.FirstName = "Praveen";  
  20.             si.LastName = "Kumar";  
  21.             si.Email = "kumar.praveen@yahoo.com";   
  22.   
  23.             Console.WriteLine(string.Format("Name : {0} {1}\nEmail : {2}", si.FirstName, si.LastName, si.Email));  
  24.             Console.ReadLine();  
  25.   
  26.         }  
  27.      }  
  28. }  

OldWay2
Figure 1.2

Newest Way of String Interpolation in C# 6.0

Now we will describe the newest way to do string interpolation/concatenation with C# 6.0 that came with Visual Studio 2015, where Visual Studio 2015 is the latest IDE of the .NET Framework.

C# 6.0 is the newest version of C#, Microsoft added a very nice feature to accelerate our program code. Now we can put expressions directly in the string literal to show the values in an actual manner. In C# 6.0 we can easily specify various formats in our values. We can do the following.

  • Place strings where we need them
  • Specify a space after/before the string
  • Use conditions

Another good thing is that we can now write a condition within the string literals with additional strings. Let's do it practically.

Example 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConsoleApplication13  
  8. {  
  9.     class StringInterpolations  
  10.     {  
  11.          public string FirstName { getset; }  
  12.          public string LastName { getset; }  
  13.          public string Email { getset; }  
  14.          public int Age { getset; }  

  15.          static void Main(string[] args)  
  16.          {  
  17.               StringInterpolations si = new StringInterpolations();  
  18.               si.FirstName = "Praveen";  
  19.               si.LastName = "Kumar";  
  20.               si.Age = 25;  
  21.       
  22.               // Putting String Where I need 
  23.               Console.WriteLine("Name : \{si.FirstName} \{si.LastName}\nEmail : \{si.Email}\nAge :\{si.Age}");  
  24.   
  25.               // Putting Space After FirstName (10 pixel)
  26.               Console.WriteLine("Name : \{si.FirstName,10} \{si.LastName}\nAge :\{si.Age :D2}");  
  27.   
  28.               //Putting Condition in string literal, If Age==25 it will display Age: Age is Over else Age in int value
  29.               Console.WriteLine("Name : \{si.FirstName, 10} \{si.LastName}\nAge :\{si.Age==25?"" :"Age is Over"}");   
  30.   
  31.               Console.ReadLine();  
  32.           }  
  33.      }  
  34. }  

/StringInterPolationC6.0
Figure 1.3

Summary

In this article we learned the new String Interpolation with C# 6.0. We saw how to put the string where we need, how to put a space after/before the string as well as how to set a condition.