In this post we will learn about “String Interpolation in C#”. It is a technique of combining, formatting, and manipulating strings. This functionality was introduced in C# 6 version. By employing string interpolation, we can incorporate objects and expressions into the string interpolation process.

Important point is format of string interpolation begins with a ‘$’ symbol and expressions or string variables are enclosed within a brace {} using the subsequent format.



Details of above Syntax

  • InterpolatedExpression: It is the expression that produces a formatted result.
  • Alignment: This value defines the minimum number of characters in the string representation of the result. If it is positive, then string representation is right-aligned and if negative, it will be left-aligned. 
  • FormatString: It is a format string that is supported by the type of the expression result.

Interpolation Sample Code

 static void Main(string[] args)
            string name = "Code config";
            string finalValue = $"Welcome to {name} !";


String Interpolation in C#

One more example of Interpolation is given below.

  static void Main(string[] args)
            string name = "Mukesh Kumar";
            string expertise = " Core";
            int year = 2023;
            string hello = $"Name of student is {name}. \n" +
            $"His is expert in {expertise }  & current year is  {year}. ";


String Interpolation in C#

String Interpolation in C# with “FormatString

        static void Main(string[] args)
            DateTime date = new DateTime();
            date = DateTime.Now;
            Console.WriteLine($"Today's date is {date:dddd, MMMM dd, yyyy} ");
String Interpolation in C#

In this post we learn about interpolation in C#. You can also check here and can Visit our website’s C# Section for more such information.

You may like:

Leave a Reply

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

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can