String interpolation is a convenient way to create strings in C# by inserting variables, expressions, and other values directly into a string.
It makes code easier to read and maintain compared with older techniques such as string concatenation or String.Format().
What Is String Interpolation?
String interpolation is indicated by placing a $ before a string:
string name = "Alice";
Console.WriteLine($"Hello, {name}!");
The $ tells C# that the string contains expressions inside { } that should be evaluated and inserted into the resulting string.
The output is:
Hello, Alice!
Without the $, C# treats {name} as ordinary text:
Console.WriteLine("Hello, {name}!");
Output:
Hello, {name}!
Basic Syntax
The general syntax is:
$"text {expression} text"
For example:
string product = "Laptop";
double price = 1299.99;
Console.WriteLine($"The {product} costs ${price}.");
Output:
The Laptop costs $1299.99.
The expression inside { } is evaluated and its result is converted to a string.
You Can Use Expressions
String interpolation is not limited to variables. You can put C# expressions inside the braces.
For example:
int width = 10;
int height = 5;
Console.WriteLine($"Area: {width * height}");
Output:
Area: 50
You can also call methods:
string name = "alice";
Console.WriteLine($"Name: {name.ToUpper()}");
Output:
Name: ALICE
And use properties:
string text = "Hello";
Console.WriteLine($"Length: {text.Length}");
Output:
Length: 5
String Interpolation vs. Concatenation
Before string interpolation, one common way to combine strings was concatenation using the + operator:
string name = "Alice";
int age = 30;
Console.WriteLine("My name is " + name + " and I am " + age + " years old.");
This works, but it can become difficult to read when a string contains many values.
With interpolation:
Console.WriteLine($"My name is {name} and I am {age} years old.");
The relationship between the text and the variables is much clearer.
Concatenation
"Hello " + name + ", you are " + age + " years old."
Interpolation
$"Hello {name}, you are {age} years old."
For most situations, interpolation is easier to read.
Formatting Numbers
String interpolation also allows you to control how values are displayed.
For example, you can format a number as currency:
double price = 1234.5;
Console.WriteLine($"Price: {price:C}");
Depending on the current culture, the output could look like:
Price: $1,234.50
You can also specify the number of decimal places:
double temperature = 23.45678;
Console.WriteLine($"Temperature: {temperature:F2}");
Output:
Temperature: 23.46
Here, F2 means fixed-point notation with two decimal places.
Formatting Dates
Interpolation is especially useful when displaying dates.
DateTime today = DateTime.Now;
Console.WriteLine($"Today is {today:yyyy-MM-dd}");
A possible output is:
Today is 2026-08-08
You can use standard and custom date/time format strings inside the braces.
For example:
Console.WriteLine($"Date: {today:dd/MM/yyyy}");
Output:
Date: 08/08/2026
Alignment and Padding
You can also control the alignment of interpolated values.
For example:
string name = "Alice";
Console.WriteLine($"|{name,10}|");
Output:
| Alice|
The 10 specifies a field width of 10 characters.
You can use a negative number for left alignment:
Console.WriteLine($"|{name,-10}|");
Output:
|Alice |
This can be useful when creating simple tables in console applications.
Combining Alignment and Formatting
You can combine alignment and formatting.
For example:
double price = 1234.567;
Console.WriteLine($"|{price,10:F2}|");
This means:
10→ use a field width of 10 charactersF2→ display two decimal places
The result is formatted accordingly.
Escaping Curly Braces
Because { and } have special meaning in interpolated strings, you need to escape them when you actually want to display curly braces.
Use double curly braces:
Console.WriteLine($"The value is {{123}}");
Output:
The value is {123}
The doubled braces tell C# that you want literal { and } characters rather than an interpolation expression.
Multiple Variables
You can insert as many expressions as you need.
string firstName = "John";
string lastName = "Smith";
int age = 25;
Console.WriteLine($"Name: {firstName} {lastName}, Age: {age}");
Output:
Name: John Smith, Age: 25
This is one of the most common uses of string interpolation.
Interpolation with Methods
You can use method calls inside interpolation:
string input = "hello";
Console.WriteLine($"Uppercase: {input.ToUpper()}");
Console.WriteLine($"Length: {input.Length}");
Output:
Uppercase: HELLO
Length: 5
However, while this is possible, very complicated expressions can make a string difficult to read.
For example, this is technically valid:
Console.WriteLine($"Result: {CalculateSomething(GetValue(x, y), z)}");
If the expression becomes complicated, it is often better to calculate the value first:
var result = CalculateSomething(GetValue(x, y), z);
Console.WriteLine($"Result: {result}");
The second version is usually easier to understand.
A Practical Example
Suppose you are writing a program that processes an input file:
string inputFile = "customers.csv";
int recordCount = 150;
Console.WriteLine($"Input: {inputFile}");
Console.WriteLine($"Records processed: {recordCount}");
The output is:
Input: customers.csv
Records processed: 150
This is a common pattern in console applications and logging:
Console.WriteLine($"Processing file: {inputFile}");
The $ makes it possible to insert the value of inputFile directly into the message.
Interpolation with Null Values
If an interpolated expression evaluates to null, its result is generally represented as an empty string.
For example:
string? name = null;
Console.WriteLine($"Name: {name}");
The output is:
Name:
You can also explicitly handle null values using the null-coalescing operator:
Console.WriteLine($"Name: {name ?? "Unknown"}");
Output:
Name: Unknown
Interpolation vs. String.Format()
Another older approach is String.Format():
string name = "Alice";
int age = 30;
Console.WriteLine(
string.Format("My name is {0} and I am {1} years old.", name, age)
);
With string interpolation, the same code is much more readable:
Console.WriteLine($"My name is {name} and I am {age} years old.");
With String.Format(), you have to remember which argument corresponds to {0}, {1}, and so on.
Interpolation lets you reference the actual variables directly.
Interpolation Is Not the Same as Concatenation
Consider:
Console.WriteLine($"Hello, {name}!");
This is called string interpolation.
On the other hand:
Console.WriteLine("Hello, " + name + "!");
is string concatenation.
Both can produce the same result, but interpolation often makes strings containing several values easier to read.
A Useful Mental Model
When you see:
$"Input: {inputFile}"
you can mentally read it as:
“Create a string saying
Input:and insert the value ofinputFilehere.”
For example, if:
inputFile = "data.csv";
then:
$"Input: {inputFile}"
becomes:
Input: data.csv
The $ activates interpolation, and {inputFile} specifies what value should be inserted.
Summary
String interpolation is one of the most useful features for working with strings in C#.
The basic pattern is:
$"Text {expression}"
Remember these key points:
$enables string interpolation.{ }contain expressions whose results are inserted into the string.- You can insert variables, properties, method calls, and calculations.
- You can format numbers and dates inside the braces.
- Use
{{and}}when you need literal curly braces. - Interpolation is often more readable than string concatenation or
String.Format().
For example:
string inputFile = "data.csv";
int records = 100;
Console.WriteLine($"Input: {inputFile}");
Console.WriteLine($"Records: {records}");
Once you understand that $ means “this string can contain expressions inside {...}“, string interpolation becomes very straightforward.