The string data type is widely used in C# and other programming languages. One of the useful features in C# is string interpolation, which was added by the Microsoft team and has proven to be a great addition to the language.

String interpolation allows developers to embed expressions and variables directly into a string. String interpolation is supported in C# 6.0 and above. It is done using the $
symbol before the opening quote of a string.
In this blog post, we’ll see the existing options available to concatenate the string. Then we will explore the benefits of using C# string interpolation with the $
symbol and how it simplifies string formatting.
Table of Contents
Existing options before string interpolation
Before using C# string interpolation, there are a few options available. Let’s explore these options with a common example.
1. String Concatenation
String concatenation is the process of combining two or more strings into a single string. It is done using the +
operator or the string.Concat()
method.
Here is an example of string concatenation:
string name = "John"; int age = 30; string message = "My name is " + name + " and I am " + age + " years old."; Console.WriteLine(message);
The output of the above code will be: My name is John and I am 30 years old.
2. String.Concat
In some cases, using String.Concat
can be more appropriate than string interpolation in C#. String.Concat
is a method that concatenates two or more strings and returns a new string that contains the concatenated text.
string name = "John"; int age = 30; string message = String.Concat("My name is ", name, " and I am ", age, " years old."); Console.WriteLine(message); // OUTPUT // My name is John and I am 30 years old.
3. StringBuilder
StringBuilder is a class in C# that is used for creating and manipulating strings. It is a more efficient way of building strings compared to using the traditional string concatenation method.
The StringBuilder class is part of the System.Text namespace and provides a mutable sequence of characters. This means that we can append, insert, remove, or replace characters in a StringBuilder object without creating a new object each time.
Here is an example of using StringBuilder to concatenate multiple strings:
string name = "John"; int age = 30; StringBuilder sb = new StringBuilder(); sb.Append("My name is " + name); sb.Append("and I am " age " years old."); string message = sb.ToString(); Console.WriteLine(message); // OUTPUT // My name is John and I am 30 years old.
4. Composite Formatting
We can use the string.Format
method for string formatting instead of traditional string concatenation. The string.Format method takes a composite format string as its first argument and a variable number of additional arguments that represent the values to be formatted.
Here is an example of using string.Format
to concatenate multiple strings:
string name = "John"; int age = 30; string message = string.Format("My name is {0} and I am {1} years old.", name, age); Console.WriteLine(message); // OUTPUT // My name is John and I am 30 years old.
As you can see, we used the {0} and {1} placeholders in the composite format string to indicate where the values should be inserted. We then passed the values to be formatted as additional arguments to the string.Format method.
C# String Interpolation
Now that we’ve seen all the old ways to do it, let’s now explore the beauty of C# string interpolation in action.
String interpolation simplifies this process by allowing us to directly embed variables and expressions inside a string. Here is an example of string interpolation:
string name = "John"; int age = 30; string message = $"My name is {name} and I am {age} years old."; Console.WriteLine(message);
The output of the above code will be the same as before: My name is John and I am 30 years old.
As you can see, string interpolation simplifies the process of string formatting by allowing us to directly embed variables and expressions inside a string, without the need for concatenation.
Using Expressions in String Interpolation
C# string interpolation allows us to use expressions directly inside a string. Here is an example:
int x = 10; int y = 20; string message = $"The sum of {x} and {y} is {x + y}."; Console.WriteLine(message); // OUPUT // The sum of 10 and 20 is 30.
Formatting with String Interpolation
C# string interpolation also allows us to format the variables and expressions that we embed in a string. We can use the same formatting options that we use with the string.Format()
method. Here is an example:
double number = 1234.5678; string message = $"The number is {number:F2}"; Console.WriteLine(message); // OUTPUT // The number is 1234.57
When string has Special characters
You may encounter situations where the string contains special characters that need to be escaped or treated differently. Here are some tips for dealing with special characters in interpolated strings:
Escape special characters using the backslash: To include a brace, “{” or “}”, in the text produced by an interpolated string, use two braces, “{{” or “}}”.
As the colon (“:”) has special meaning in an interpolation expression item, to use a conditional operator in an interpolation expression, enclose that expression in parentheses.
The following example shows how to include a brace in a result string. It also shows how to use a conditional operator:
string name = "John"; int age = 30; Console.WriteLine($"He asked, \"Is your name {name}?\", but didn't wait for a reply :-{{"); Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old."); // OUPUT // He asked, "Is your name Horace?", but didn't wait for a reply :-{ // Horace is 30 years old.
FAQ
Does C# have string interpolation?
Yes, C# has string interpolation, which is a introduced in C# 6.0.
How to interpolate variables in strings in C#?
Use the $ symbol before the string literal and enclose the variable or expression we want to interpolate in curly braces {}.
Example: var name = “John”; Console. WriteLine($”Hello, {name}”);
When to use string interpolation C#?
C# string interpolation is used to format and manipulate strings.
How is string interpolation performed?
String interpolation in C# is performed using the $ symbol before the string literal. The string literal can include placeholders enclosed in curly braces {} that are replaced with the values of expressions or variables.
Conclusion
C# string interpolation simplifies the process of string formatting by allowing us to directly embed variables and expressions inside a string, without the need for concatenation.
We can also use expressions and format the variables and expressions that we embed in a string.
String interpolation is a powerful feature of C# that can make our code more readable and maintainable.