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.
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace StringInterpolation
- {
- class StringInterpolation
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
-
- static void Main(string[] args)
- {
-
-
- StringInterpolation si = new StringInterpolation();
- si.FirstName = "Praveen";
- si.LastName = "Kumar";
- si.Email = "[email protected]";
-
- Console.WriteLine("Name : "+ si.FirstName+" "+si.LastName+"\nEmail : "+si.Email);
- Console.ReadLine();
- }
- }
- }
Figure 1.1
Example: Old way to concatenate strings using a placeholder.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace StringInterpolation
- {
- class StringInterpolation
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
-
- static void Main(string[] args)
- {
-
-
- StringInterpolation si = new StringInterpolation();
- si.FirstName = "Praveen";
- si.LastName = "Kumar";
- si.Email = "[email protected]";
-
- Console.WriteLine(string.Format("Name : {0} {1}\nEmail : {2}", si.FirstName, si.LastName, si.Email));
- Console.ReadLine();
-
- }
- }
- }
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
Another good thing is that we can now write a condition within the string literals with additional strings. Let's do it practically.
Example
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApplication13
- {
- class StringInterpolations
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
- public int Age { get; set; }
- static void Main(string[] args)
- {
- StringInterpolations si = new StringInterpolations();
- si.FirstName = "Praveen";
- si.LastName = "Kumar";
- si.Age = 25;
-
- // Putting String Where I need
- Console.WriteLine("Name : \{si.FirstName} \{si.LastName}\nEmail : \{si.Email}\nAge :\{si.Age}");
-
- // Putting Space After FirstName (10 pixel)
- Console.WriteLine("Name : \{si.FirstName,10} \{si.LastName}\nAge :\{si.Age :D2}");
-
- //Putting Condition in string literal, If Age==25 it will display Age: Age is Over else Age in int value
- Console.WriteLine("Name : \{si.FirstName, 10} \{si.LastName}\nAge :\{si.Age==25?"" :"Age is Over"}");
-
- Console.ReadLine();
- }
- }
- }
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.