C# String Interpolation

C# Code Snippets String Interpolation


In this code snippet, we will take a look at string interpolation in C#.

To enable string interpolation put the $ character before the string. Now variables in brackets can be put straight into a string. This is a bit more convenient compared to splitting up as string, putting the desired variables in between the strings and than concatenating it all together.

Let’s have a look at the code below to see how to use string interpolation.


using System;

namespace stringInterpolation
    class Program
        static void Main(string[] args)
            int a = 10;

            //Both ways do the same thing. The second one with string interpolation is just a bit more convinient.

            //You can do this or...
            Console.WriteLine("The value of a is " + a + ". ");

            //use string interpolation and to this. 
            Console.WriteLine($"The value of a is {a}");

            //You should place your variables inside a set of curly braces. And use the "$" infront of your string to enable interpolation.


Resulting output:


Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger