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 easily. During the development period, formatting the string
values is 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 string
s 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 string
s that now became the old way to do string
concatenation. String
interpolation replaces the need for string.Format(...)
by using escaped curly braces, \{...} , inside string
s. With the release of C# 6.0, we can rewrite the string
s 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.
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:
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); Console.WriteLine(stringValue1);
string stringValue2 = "\{value1} \{value2}"; 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 = "abhishek.arora22@gmail.com";
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 = "abhishek.arora22@gmail.om";
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 tip, 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 you will use it in your project? Your comments are most welcome.