String Interpolation and it's internals in C#

Anup1252000
Posted by in C# category on for Intermediate level | Points: 250 | Views : 683 red flag
Rating: 4 out of 5  
 1 vote(s)

String Interpolation is introduced in C# 6.0 . String Interpolation is the feature to insert value(s) into the string.


 Download source code for String Interpolation and it's internals in C#

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. 

Lets 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

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!
Page copy protected against web site content infringement by Copyscape

About the Author

Anup1252000
Full Name: anup hosur
Member Level:
Member Status: Member
Member Since: 8/1/2009 2:37:23 AM
Country: India
https://wordpress.com/pages/anuphosur.wordpress.com
https://wordpress.com/pages/anuphosur.wordpress.com
He is the .net developer with around 8 years of experience in technologies like c#, WCF, WPF and Asp.net. https://wordpress.com/pages/anuphosur.wordpress.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)