String Interpolation

What is string interpolation in c#?
String interpolation provides a more readable, convenient syntax to format strings. It''s easier to read than string composite formatting. It allows us to insert variables into the string. It uses simple syntax and looks like a template. An interpolated string returns a string as a result.

Curly braces ({}) are used to enclose and separate variables into the interpolated string.

$"<text> {<interpolated-expression> [,<field-width>] [<:format-string>] } <text> ..." 


using System;  
namespace StringInterpolation
    class StringInterpolationClass
        public static void Main()  
            var name = "Mark";  
            var age  = 42;  
            // Composit format string  
            Console.WriteLine("Name = {0}, age = {1}", name, age);  
            // String Interpolation  
            var stringinterpolation   = $"{name} is {age} years old.";  


Name = Mark, age = 42
Mark is 42 years old.

0 Comment's

Comment Form