About
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.
Code:
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. Console.ReadLine(); } } }