String Interpolation and it’s internals

You can download the code from here Source code

String Interpolation is introduced in C# 6.0 . String Interpolation is the feature to insert value(s) into the string. Confused?
Lets start with string formatter
Before c#6.0, we used to write something like this

string log = string.Format(“Values for the sum are value1:{0} and Value2:{1}”, value1, value2);

But with the new .net framework, it reduced the effort of writing long code. Let’s see how we can write string interpolation.

string log = $”Values for the sum are value1:{value1} and Value2:{value2}”;

Thumb rule is you have always start with ‘$’ symbol & then quotes and value(s) to display.
Let’s take a simple example of calculating the sum of 2 integers. Here is the code snippet

private static int CalculateSum(int value1, int value2)
{
string log = $”Values for the sum are value1:{value1} and Value2:{value2}”;
Console.WriteLine(log);
return value1 + value2;
}

static void Main(string[] args)
{
int sum = CalculateSum(5, 6);
Console.WriteLine($”sum is:{sum}”);
Console.ReadLine();
}

Looks simple right!.

Wait, you might be wondering what’s the difference between string.format and string interpolation. correct? Let’s check the decompiled code of calculatesum code snippet in dotpeek tool.

Note: There are many decompiler tool available ex: Reflector, ILspy etc. To know more about how dotpeek works, please refer to the below link
DotPeek Tutorial

internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(string.Format(“sum is:{0}”, (object) Program.CalculateSum(5, 6)));
Console.ReadLine();
}

private static int CalculateSum(int value1, int value2)
{
Console.WriteLine(string.Format(“Values for the sum are value1:{0} and Value2:{1}”, (object) value1, (object) value2));
return value1 + value2;
}

So, here the conclusion is string.format and string interpolation are one and the same. Internally CLR will treat string interpolation as string.format.
One more question might be coming in your mind , whether the string interpolation is only relevant to data types like int, string, float etc.
Answer is no. you can use string interpolation with array, method, expression and so on.
Let’s see how we can use string interpolation with Method and Expression

Below is the code snippet for the Expression

private static string Multiplication(int value1,int value2)
{
return $”Multiplication of {value1} * {value2} is {value1 * value2}”;
}
static void Main(string[] args)
{
string multiplication = Multiplication(10, 10);
Console.WriteLine(multiplication);
Console.ReadLine();
}

Let’s see how we can do string interpolation for methods

private static int Substract(int value1,int value2)
{
return value1 – value2;
}
Console.WriteLine($”substraction of 7-5 is {Substract(7,5)}”);

Hope you enjoy the article. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s