Introduction
On November 12, 2014, the day of the Visual Studio Connect() event, Microsoft announced a 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.
Don't forget to read my previous posts on this series: "A new feature of C# 6.0"
- Exception Filtering: A New Feature of C# 6.0
- Nameof Operator: A New Feature of C# 6.0
- Auto Property Initializer: A New Feature of C# 6.0
- Using Static Class Statements: A New Feature of C# 6.0
- Dictionary Initializers: A New Feature of C# 6.0
- Using Await in Catch and Finally Blocks: A New Feature of C# 6.0
- Expression Bodied Members: A New Feature of C# 6.0
- Null Propagation Operator: A New Feature of C# 6.0
What is String Interpolation?
String Interpolation is another cool feature that helps you to manage string formatting easily. During the development period, formatting the string values is very common. While this is common, it may lead to errors because every time 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 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 placeholders {n}, and based on the number we need to set the variables.
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, which can be looked at as an improvement. Therefore, the preceding code snippet can be re-written as.
var message = $"Book Name: \{name} Price: \{price}";
Example 2
public static void Main(string[] args)
{
int a = 5, b = 10;
Console.WriteLine(" The value A is \{a} while B is \{b}");
Console.ReadKey();
}
Example 3
public static void Main(string[] args)
{
string value1 = "Abhishek";
string value2 = "Arora";
string stringValue1 = string.Format("{0} {1}", value1, value2); // With string.Format
Console.WriteLine(stringValue1);
string stringValue2 = $"{value1} {value2}"; // With String Interpolation
Console.WriteLine(stringValue2);
}
Demo Application using Visual Studio 2013.
using System;
using System.Text;
namespace CSharpFeatures
{
class StringInterpolation
{
public string Name { get; set; }
public string Location { get; set; }
public string Age { get; set; }
public string Email { get; set; }
static void Main(string[] args)
{
StringInterpolation s = new StringInterpolation();
s.Name = "Abhishek Arora";
s.Location = "Ghaziabad";
s.Age = "23";
s.Email = "[email protected]";
Console.WriteLine("\n -- Old way of string concatenation using '+' Operator --");
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);
Console.WriteLine("\n\n -- Old way of string concatenation using String.Format Method --");
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));
Console.ReadKey();
}
}
}
Demo Application using Visual Studio 2015 Preview.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpFeatures
{
class StringInterpolation
{
public string Name { get; set; }
public string Location { get; set; }
public string Age { get; set; }
public string Email { get; set; }
static void Main(string[] args)
{
StringInterpolation s = new StringInterpolation();
s.Name = "Abhishek Arora";
s.Location = "Ghaziabad";
s.Age = "23";
s.Email = "[email protected]";
Console.WriteLine("\n -- using String Interpolation Method with VS 2015 preview --");
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}");
Console.WriteLine("\n\n -- Condition Checking -- ");
int totalamount = 2500;
Console.WriteLine("\n Congrats \{s.Name} \{totalamount == 2500 ? "you are eligible for 10% discount!" : " No Discount"}");
Console.ReadKey();
}
}
}
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.

SubashPosted Sep 28, 2016, 12:29 AM
Good feature
Rajeesh MenothPosted Aug 11, 2015, 7:01 AM
Good One
Gowtham RajamanickamPosted Apr 2, 2015, 1:15 AM
great
Onlymorons onthiswebsitePosted Feb 3, 2015, 9:31 AM
What a bad feature, no decent developer should use this syntax. Why would Microsoft promote this kind of programming?
Rajeev RanjanPosted Dec 31, 2014, 7:13 AM
such you took simple examples and the way you write this article is awsm, keep it up!
Humayun Kabir MamunPosted Dec 31, 2014, 5:56 AM
Nice info...
Harieswaran DPosted Dec 31, 2014, 4:52 AM
nice
Guest UserPosted Dec 31, 2014, 3:30 AM
Sam Hobbs can you please share key performance implications if you've already read the details?
Guest UserPosted Dec 31, 2014, 3:28 AM
I say it's syntactic sugar for developers. @Sam Hobb
Atul RawatPosted Dec 31, 2014, 1:19 AM
nice article
Shridhar SharmaPosted Dec 31, 2014, 1:10 AM
good job.
Sam HobbsPosted Dec 31, 2014, 12:38 AM
Thank you for telling us about this, Abhishek. However apparently not all the experts agree that the new string interpolation is wonderful. In fact there is enough material for at least one more article. See: https://roslyn.codeplex.com/discussions/540869
Rizwan AliPosted Dec 31, 2014, 12:33 AM
good one article
Vithal WadjePosted Dec 31, 2014, 12:27 AM
good feature