String Interpolation

This feature inserts values into a string with simple syntax. It is similar to string.Format, but variables may be accessed directly (not through index arguments)

C# program that uses string interpolation

using static System.Console;

class Program
{
    static void Main()
    {
        int cats = 100;
        int dogs = 2;

        // Create a string with string interpolation syntax.
        string animals = $"cats = {cats} and dogs = {dogs}";
        WriteLine(animals);
    }
}

Output

cats = 100 and dogs = 2

Leave a comment