String Interpolation in the latest C# language

259 0

Before the release of  C# 6.0, we typically concatenated two or more strings together with  the following methods:

static void Main(string[] args)
{
string firstName = "David";
string lastName = "Z";
Console.WriteLine("Name : " + firstName + " " + lastName);
Console.WriteLine("Name : {0} {1}", firstName, lastName);

Console.ReadLine();
}

With C# 6.0, we have a cleaner way to format a string by writing our own arguments instead of referring to them as placeholders.

static void Main(string[] args)
{
string firstName = "David";
string lastName = "Z";

WriteLine($"I am {firstName} {lastName} !");

ReadLine();
}

As you can noticed the introduction of the dollar $ sign.
The result output is the same
I am David Z !

Just make sure you use the $ before the start of the string.

Related Post