If you are new to C# or have been learning C# string from quite some time, then you may stumble upon, string interpolated version in C# ($"Hello, {YourName}"), which was introduced in C# 6, so in this article, I have explained about c# string interpolated version with console application examples.

What is string interpolation?

In Programming language, string interpolation, is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

So we can say, an interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results.

Available in C# 6.0 and later, Interpolated strings are identiified by $ special character, take a look at an example

 class Program
    {
        static void Main(string[] args)
        {
            var name = "Vikas Lalwani";
            Console.WriteLine($"My name is {name}");
            Console.ReadKey();
        }
    }

Output of the above code will be

My name is Vikas Lalwani

Here is the image below which shows output, when code is written in Visual Studio using console application.

c-sharp-string-interpolation-example-min.png

In C#, a string literal is an interpolated string, when you prepand it with the $ symbol. You cannot have any white space between the $ and the " that starts a string literal.

Interpolated String Example in C#

Let's take a look at more complex example of interpolated string, suppose, you want to ask the name and age from user, then show it as an output on console.

namespace StringInterpolation
{
    class Program
    {
        static void Main(string[] args)
        {
            string name="", age = "";

            Console.WriteLine("What is your name?");
            //assign value to name variable
            name=Console.ReadLine();

            Console.WriteLine("What is your age?");
            //assign value to age variable
            age = Console.ReadLine();

            //print it to console using interpolated string 
            Console.WriteLine($"Your name is {name} and age is {age}");

            Console.ReadKey();
        }
    }
}

Output:

What is your name?
John Kanhwald
What is your age?
20
Your name is John Kanhwald and age is 20

output-interpolated-string-c-sharp-min.png

Adding Special characters inside interpolated string in C#

Suppose you want to add special characters like "; {" etc inside interpolated string, then you can do it like

string name = "John";
Console.WriteLine($"Hello, \"are you {name}?\", but not the terminator movie one :-{{");

Output:

Hello, "are you John?", but not the terminator movie one :-{

Expression Evaluation

With string interpolation, expressions within curly braces {} can also be evaluated. The result will be inserted at the corresponding location within the string.

let's take a look at an example

Console.WriteLine($"The greater one is: { Math.Max(10, 20) }");
Console.WriteLine($"Today's day and date is: {DateTime.Today:dddd, dd-MM-yyyy}");

Output:

The greater one is: 20
Today's day and date is: Tuesday, 01-09-2020

Method call

Yes, you can also call methods, let them evaluate and return results. The returned result will be placed in the corresponding location.

        static void Main(string[] args)
        {
            Console.WriteLine($"The 5*5  is {MultipleByItSelf(5)}");
        }
      
        static int MultipleByItSelf(int num)
        {
            //multiply number by itself and return result
            return num * num;
        }

Output:

The 5*5  is 25

You may also like to read:

C# Datetime

Generic class in C#