All the Ways to Do String Interpolation in C#
C# string interpolation is the cleanest way to build a string from values. You prefix a string with $, drop your expressions inside {}, and the compiler stitches the result together. It reads better than concatenation and runs faster than you might expect. This guide walks through every variant, from the basic $"" to interpolated string handlers, and points out the two places where interpolation will hurt you if you reach for it by habit.
What Is String Interpolation?
String interpolation lets you embed expressions directly inside a string literal. Prefix the string with $, wrap each expression in curly braces, and the runtime replaces the braces with the expression's value.
Csharp
string name = "Ada";
int count = 3;
string message = $"{name} has {count} messages.";
// "Ada has 3 messages." Anything that is a valid expression works inside the braces, including method calls, arithmetic, and conditionals.
Csharp
string status = $"You have {count} {(count == 1 ? "message" : "messages")}.";
// "You have 3 messages." The rest of this guide covers control: formatting, alignment, culture, and performance.
Format Specifiers
Add a colon after an expression to apply a standard or custom format string. This is the same formatting syntax string.Format uses.
Csharp
decimal price = 1234.5m;
DateTime now = new(2026, 10, 1, 14, 30, 0);
string formatted = $"Total: {price:C}"; // "Total: $1,234.50"
string percent = $"Rate: {0.0825:P2}"; // "Rate: 8.25%"
string date = $"Date: {now:yyyy-MM-dd}"; // "Date: 2026-10-01"
string hex = $"Id: {255:X4}"; // "Id: 00FF" The format specifier respects the current culture for currency, percentages, and dates. More on culture below, because it is a common source of bugs.
Nested Interpolation Gotchas
When an expression inside {} contains ternary operators or nested quotes, you need parentheses and double quotes. If you nest interpolated strings inside expressions, inner braces and quotes must be handled carefully or avoided using raw string literals.
Csharp
bool isValid = true;
string user = "Ada";
// Parentheses are required around ternary operators containing colons
string label = $"Status: {(isValid ? "Active" : "Inactive")}";
// Nested interpolation inside an expression
string greeting = $"Hello, {($"User: {user}")}"; Alignment
Add a comma and a number to pad a value to a minimum width. Positive numbers right-align, negative numbers left-align. This is how you build readable columns without a table.
Csharp
string[] items = { "Apples", "Bread", "Coffee" };
decimal[] costs = { 3m, 2.5m, 12.99m };
for (int i = 0; i < items.Length; i++)
{
// Left-align the name in 10 chars, right-align the cost in 8
Console.WriteLine($"{items[i],-10}{costs[i],8:C}");
}
// Apples $3.00
// Bread $2.50
// Coffee $12.99 You can combine alignment and a format specifier: {value,8:C} means "right-align in 8 characters, formatted as currency." Alignment comes first, then the colon and the format.
Escaping Braces
To write a literal curly brace, double it. This matters when you are generating code, JSON, or anything brace-heavy.
Csharp
int value = 42;
string json = $"{{ \"value\": {value} }}";
// { "value": 42 } Verbatim Interpolated Strings (C# 6+)
Combine $ with the verbatim @ prefix to interpolate across multiple lines and keep backslashes literal. The order does not matter: both $@"" and @$"" work.
Csharp
string folder = "reports";
string path = $@"C:\data\{folder}\output.csv";
// C:\data\reports\output.csv
string block = $@"User: {name}
Messages: {count}"; Raw String Literals With Interpolation (C# 11+)
Raw string literals (C# 11+) remove the need to escape quotes and backslashes, which makes them a great fit for JSON, SQL text, or regex patterns. You can interpolate into them too.
Csharp
string user = "Ada";
int id = 7;
string json = $$"""
{
"user": "{{user}}",
"id": {{id}}
}
"""; In a raw interpolated string, the number of $ signs sets how many braces mark an interpolation. One $ uses {...}; two $$ uses {{...}}. This keeps the single braces in your JSON literal while {{user}} interpolates. Add more $ if your content itself contains doubled braces.
How Do You Control Culture?
By default, interpolation formats values using the current thread culture. That works for user-facing text but not for machine-readable output like JSON, URLs, or file formats, where you want a stable, culture-independent result.
An interpolated string is implicitly convertible to both string and FormattableString. Capture it as FormattableString to defer formatting and pick a culture.
Csharp
using System.Globalization;
decimal amount = 1234.5m;
// Culture-dependent: "1.234,50" in de-DE, "1,234.50" in en-US
string local = $"{amount:N2}";
// Culture-independent, stable across machines
string invariant = FormattableString.Invariant($"{amount:N2}");
// "1,234.50"
// Or a specific culture
FormattableString fs = $"{amount:C}";
string german = fs.ToString(new CultureInfo("de-DE")); Reach for FormattableString.Invariant(...) whenever a machine will parse the string instead of a person reading it.
Interpolated String Handlers (.NET 6+ / C# 10+)
Since .NET 6 and C# 10, the compiler does not always build a single formatted string up front. When a method accepts an interpolated string handler, the compiler passes the literal parts and the expressions separately, and the handler decides whether to build the string at all. This avoids allocations when the result is never used.
StringBuilder.Append is the classic example. It appends each piece directly instead of allocating an intermediate string.
Csharp
var sb = new System.Text.StringBuilder();
sb.Append($"User {name} has {count} messages.");
// No intermediate formatted string is allocated. The same pattern powers conditional formatting. Debug.Assert only builds the message when the condition fails, so the interpolation cost disappears on the happy path.
Csharp
System.Diagnostics.Debug.Assert(count >= 0, $"Count was negative: {count}"); You can write your own handler for hot paths, but most developers benefit from this without doing anything: the runtime APIs you already call now use handlers.
How Does Interpolation Compare to the Alternatives?
For readability, interpolation beats concatenation and string.Format in almost every case. For performance, it is competitive and often faster, because the compiler can choose an efficient handler instead of boxing arguments into an object[] the way string.Format does.
Csharp
// Concatenation: fine for one value, noisy for several (allocates intermediate strings)
string a = "User " + name + " has " + count + " messages.";
// string.Format: positional, easy to misorder, boxes value types (object[] allocation)
string b = string.Format("User {0} has {1} messages.", name, count);
// Interpolation: reads in order, no boxing with handlers, zero intermediate allocations
string c = $"User {name} has {count} messages."; Use StringBuilder when you are appending in a loop. Use interpolation everywhere else.
Where Should You Avoid Interpolation?
Interpolation builds a finished string. Some APIs want a template with values kept separate, and interpolating into it breaks the feature they provide. These are the two most common cases for mistakes.
Logging. Structured loggers expect a message template with named placeholders so they can capture each value as a field. Interpolate into the template, and you throw away the structure and pay the formatting cost even when the log level is disabled.
Csharp
// Wrong: formats eagerly, loses structured fields
logger.LogInformation($"User {name} logged in with {count} sessions");
// Right: the logger captures name and count as queryable fields
logger.LogInformation("User {Name} logged in with {Count} sessions", name, count); SQL. Interpolating user input into a SQL string is a classic injection vulnerability. Use parameters. (EF Core's FromSqlInterpolated is the exception: it reads the interpolated string and turns each value into a parameter for you.)
Csharp
// Dangerous: raw interpolation into SQL string causes SQL Injection vulnerability
var sql = $"SELECT * FROM Users WHERE Name = '{name}'";
// Safe: EF Core's FromSqlInterpolated converts expressions into parameterized SQL queries
var users = context.Users
.FromSqlInterpolated($"SELECT * FROM Users WHERE Name = {name}")
.ToList(); Both cases follow one rule: if another system consumes the string as a template, let it handle the values.
Frequently Asked Questions
What is string interpolation in C#? It is a syntax that embeds expressions inside a string literal. Prefix the string with $ and wrap each expression in {}; the compiler produces a string with the values substituted in.
How do I format a value inside an interpolated string? Add a colon and a format string after the expression, like {price:C} for currency or {date:yyyy-MM-dd} for a date. The syntax matches string.Format.
How do I keep interpolation from using the local culture? Wrap the interpolated string with FormattableString.Invariant(...), or capture it as a FormattableString and call ToString with the culture you want.
Is string interpolation slower than concatenation? No. For multiple values it is competitive with or faster than concatenation and string.Format, because the compiler can use an interpolated string handler and avoid boxing.
Call to Action
Open your current project and search for string.Format and string concatenation. Most of those are clearer as interpolation, and a few in your logging calls are bugs worth fixing today.