String Interpolation And Its Internals

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

Let's start with string formatter.

Before C#6.0, we used to write, as shown below.

  1. string log = string.Format("Values for the sum are value1:{0} and Value2:{1}", value1, value2);  
With the new .NET framework, it reduced the effort of writing long code. Let's see how we can write string interpolation.
  1. string log = $"Values for the sum are value1:{value1} and Value2:{value2}";  
Rule of thumb is you need to always start with '$' symbol followed by quotes and value(s) to display.

Let's take a simple example of calculating the sum of 2 integers. The code snippet is mentioned below.
  1. private static int CalculateSum(int value1, int value2)  
  2. {  
  3.     string log = $ "Values for the sum are value1:{value1} and Value2:{value2}";  
  4.     Console.WriteLine(log);  
  5.     return value1 + value2;  
  6. }  
  7.   
  8. static void Main(string[] args) {  
  9.     int sum = CalculateSum(5, 6);  
  10.     Console.WriteLine($ "sum is:{sum}");  
  11.     Console.ReadLine();  
  12. }  
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 tools available, for example, Reflector, ILspy etc. To know more about how dotpeek works, please refer to the link, mentioned below.
  1. internal class Program  
  2. {  
  3.     private static void Main(string[] args) {  
  4.         Console.WriteLine(string.Format("sum is:{0}", (object) Program.CalculateSum(5, 6)));  
  5.         Console.ReadLine();  
  6.     }  
  7.   
  8.     private static int CalculateSum(int value1, int value2) {  
  9.         Console.WriteLine(string.Format("Values for the sum are value1:{0} and Value2:{1}", (object) value1, (object) value2));  
  10.         return value1 + value2;  
  11.     }  
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 in your mind, whether the string interpolation is only relevant to data types like int, string, float etc. or not.

Answer is no. You can use string interpolation with an 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.
  1. private static string Multiplication(int value1, int value2)  
  2. {  
  3.     return $ "Multiplication of {value1} * {value2} is {value1 * value2}";  
  4. }  
  5. static void Main(string[] args) {  
  6.     string multiplication = Multiplication(10, 10);  
  7.     Console.WriteLine(multiplication);  
  8.     Console.ReadLine();  
  9. }  
Let's see how we can do string interpolation for the methods.
  1. private static int Substract(int value1, int value2) {  
  2.     return value1 - value2;  
  3. }  
  4. Console.WriteLine($ "substraction of 7-5 is {Substract(7,5)}");  
Hope you enjoyed the article. Cheers.

 

Ebook Download
View all
Learn
View all