We all have a moment when we have a string and want to add a variable’s value to that string. There are many ways to accomplish this. However, the best way to add values of variables to a string is done with C# string interpolation. It makes the string easier to read and simplifies the syntax.
Table Of Contents
C# String Interpolation In Short
You have 2 variables; a movie name and a rating. With these variables, you want to write ‘The movie NAME has a rating of RATING’. How do you replace NAME and RATING with the actual name and rating of the movie? The most common way is this:
string movie = "Shrek";
int rating = 9;
Console.WriteLine("The movie " + movie + "has a rating of " + rating);
I think you have seen or done this a lot. And it’s not a bad way to write a string with variables. But did you already spot the error? If I run the application, it will look like this:

The movie name is attached to ‘has’. It’s an easy mistake since it’s not easy to see the spaces in the code. We can avoid this by using the C# string interpolation.
We could also use the String.Format, which is better than just putting pieces of code together;
string movie = "Shrek";
int rating = 9;
Console.WriteLine(string.Format("The movie {0} has a rating of {1}", movie, rating));
It’s better, but still not the best way.
C# string interpolation was introduced in C# 6.0 and it was a new way of creating more readable formatted strings, with better syntax than with the more traditional methods like String.Format, and it looks like this:
string movie = "Shrek";
int rating = 9;
Console.WriteLine($"The movie {movie} has a rating of {rating}");
It looks a bit like the String.Format, but with the variables inside the string, not outside it as an array or parameters. The dollar sign ($) in front of the string makes the string ready for string interpolation.
Fun fact; Before C#10, the string interpolation was compiled to a String.Format. Since C# 10 this is no longer the case. String interpolation is now compiled to a DefaultInterpolatedStringHandler.
Formatting
One of the greatest things you can do with C# string interpolation is formatting. You can format dates and numbers within the interpolation.
For example a date. I live in Europe and the date notation here is dd-MM-yyyy. The way C# has been formatting dates hasn’t always been easy, but they made it much better over the years. Anyway, to format a Date type to the format with string interpolation, you simply write your code like this:
DateTime dateTime = DateTime.Now;
Console.WriteLine($"Today is {dateTime:dd-MM-yyyy hh:mm:ss}");
You can also format numbers. It’s pretty simple to adjust the number of decimals to a variable before writing it in a string. This is ideal if you are working with currency.
float result = 100f / 3f;
Console.WriteLine($"The whole number with decimals is {result}.");
Console.WriteLine($"With formatting it is {result:.##}");
The number of hashes tells how many digits you want to use. Instead of hashes, you can use an F with a number. You can increase or decrease the number because it indicates how many digits behind the comma will be shown.
float result = 100f / 3f;
Console.WriteLine($"The whole number with decimals is {result}.");
Console.WriteLine($"With formatting it is {result:F2}");
Conditions
It is possible to add conditions to a string interpolation. This means you can have an if statement inside the brackets. You might want to use this if you have a small condition. I wouldn’t use a full-grown if-statement, since this is not possible and if it was possible, it would make the code unreadable.
int rating = 10;
string movieTitle = "Shrek";
bool seen = false;
Console.WriteLine($"You have {(seen ? "seen" : "not seen")} The movie {movieTitle}.");
Do note that you need to use the parentheses ( the ‘(‘ and ‘)’) to make this work. If the variable seen is true, the text will be “You have seen the movie Shrek.”. When the value of seen is false, the text will be “You have not seen the movie Shrek.”
Another example of conditions in string interpolation:
int rating = 10;
string movieTitle = "Shrek";
Console.WriteLine($"You have rated the movie {movieTitle} as {(rating > 8 ? "good" : "not good")}.");
If you need to check more than one condition, you can use expressions.
Using Expressions
Although I am not sure this is a really good and readable feature, you can use expressions inside the string interpolation. It is an expression C# switch. This is a nice way of making decisions inside the string interpolation.
int rating = 10;
string movieTitle = "Shrek";
string message = $"The movie {movieTitle} was rated {rating switch
{
>=1 and <5 => "bad",
>= 5 and <8 => "good",
>=8 => "perfect"
}}";
Console.WriteLine(message);
Inside the string, I check if the rating is between 1 and 5, 5 and 8, and 8 or higher.
Culture-Specific
One of the things we developers hate; cultural differences. Not in the sense of actual cultures, but the way different cultures write dates or numbers. Especially large numbers can be difficult. Some cultures write with dots as thousand separates, whereas others use commas. Let’s take the distance to the moon, which is 384400 kilometers.
To add culture-specific settings to the C# string interpolation, you create a new string with string.Create. This gets two parameters; the culture and the string. Here is an example of how the culture settings change the way the string is written:
double distanceToTheMoon = 384.400;
var specificCulture = System.Globalization.CultureInfo.GetCultureInfo("nl-NL");
string messageInSpecificCulture = string.Create(specificCulture, $"The distance to the moon is {distanceToTheMoon:N3}.");
Console.WriteLine( messageInSpecificCulture );
specificCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
messageInSpecificCulture = string.Create(specificCulture, $"The distance to the moon is {distanceToTheMoon:N3}.");
Console.WriteLine(messageInSpecificCulture);

The first line is for The Netherlands and the decimal is a comma, which is common here. On the second line, you see the culture setting for the USA, which has a dot as a decimal separator.
By specifying which culture I want to use it can change a lot in the way your application writes information. It even could change the way it stores information in a database or file. Know what culture you want to use if it is really important.
Benchmarking C# String Interpolation
I have benchmarked the string interpolation and a ‘simple’ string.Format. This is the code I used to benchmark:
public class Benchmarks
{
private string movieTitle = "Shrek";
private int rating = 0;
private bool seen = true;
[Benchmark]
public void UsingStringFormat()
{
string result = string.Format("You have rated the movie {0} with a {1} and you have {2} it.", movieTitle, rating, (seen ? "seen" : "not seen"));
Console.WriteLine(result);
}
[Benchmark]
public void UsingStringInterpolation()
{
string result = $"You have rated the movie {movieTitle} with a {rating} and you have {(seen ? "seen" : "not seen")} it.";
Console.WriteLine(result);
}
}
This is the result:

The method UsingStringInterpolation has a lower mean and median, making it a little bit better in performance. But the differences are really small. I wouldn’t say one is better in performance than the other.
Conclusion On C# String Interpolation
Using C# string interpolation makes it easier to concatenate strings in C#. It’s better for readability and syntax. Although the string interpolation has a bit better performance, there are still very valid reasons why to use the string.Format or other ways to concatenate a string in C#. C# string interpolation is easier to read, the syntax is better, and there are more possibilities within the interpolation.
Test Your Knowledge
[klccampaign id=8533b7df-22fd-448a-ac62-a7c6ffb053df]





