Skip to main content

How to Insert Values into a String with String Interpolation in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Building a sentence out of values, such as a greeting with a name and a count, is one of the most common things a program does. In C#, a $ before a string lets you write the values straight into it, between curly braces.

Syntax​

Syntax
$"<text>{<expression>}<text>"
$"<text>{<expression>,<width>:<format>}<text>"
PartWhat it does
$Marks the string as an interpolated string. It goes right before the opening quote.
{<expression>}Replaced by the expression's value as text. The expression can be a variable, a calculation such as count * 2, or a method call.
,<width>Optional. The minimum number of characters: a positive width right-aligns the value, a negative one left-aligns it.
:<format>Optional. A format string for the value's type, such as N2 for a number with two decimals.
{{ and }}A literal { or } in the text.

Microsoft's string interpolation reference: "Use the $ character to identify a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions."

The Example​

The example greets Leo, who has three new messages, works out double that number inside the string, and prints a line that contains literal braces.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which sets name to Leo and count to 3, then prints three interpolated strings: a greeting with the name and the count, a sentence with count times 2, and a sentence that writes literal braces with doubled braces.

main.cs
var name = "Leo";
var count = 3;

Console.WriteLine($"Hi {name}, you have {count} new messages.");
Console.WriteLine($"Next year you'll have {count * 2} if this goes on.");
Console.WriteLine($"Use {{ and }} to write braces.");

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Hi Leo, you have 3 new messages.
Next year you'll have 6 if this goes on.
Use { and } to write braces.

A zsh terminal where dotnet run main.cs prints Hi Leo, you have 3 new messages., Next year you'll have 6 if this goes on. and a line with single curly braces in it.

Each {...} was replaced by its value: Leo, 3, and 6, which C# calculated from count * 2. The doubled braces came out as single ones. The command is the same in zsh, bash and PowerShell.

What Happens Without the $​

Leave out the $ and the braces are just text:

main.cs
var name = "Leo";
Console.WriteLine("Hi {name}!");
Output
/Users/you/demo/main.cs(1,5): warning CS0219: The variable 'name' is assigned but its value is never used
Hi {name}!

The program still runs, but it prints {name} as typed. The warning is the clue: the compiler noticed that name is never read, because the string no longer refers to it.

What Happens with a Single Brace​

A single { inside an interpolated string always starts an expression. $"Use { and } to write braces." makes C# look for something called and:

Output
/Users/you/demo/main.cs(1,27): error CS0103: The name 'and' does not exist in the current context

The build failed. Fix the build errors and run again.

Double the braces, {{ and }}, to print them.

Common Mistakes​

MistakeWhat happensDo this instead
Forgetting the $The braces print as typed, and warning CS0219 says the variable is never usedPut $ right before the opening quote
Writing a single { or } for a literal braceThe text inside is read as an expression, such as error CS0103: The name 'and' does not exist in the current contextWrite {{ or }}
Putting a conditional operator straight inside the bracesThe colon is read as the start of a format stringWrap the expression in parentheses, as in {(count > 1 ? "messages" : "message")}

Troubleshooting​

SymptomLikely causeFix
The output shows {name} literallyThe $ is missingAdd $ before the string
CS0103 for a word inside a stringA single brace turned the text into an expressionDouble the brace

FAQ​

What does a null value print?​

Nothing. The reference: "When the expression is null, the output is the empty string".

Can I put a conditional operator inside the braces?​

Yes, in parentheses. The reference: "To use a conditional operator in an interpolation expression, enclose that expression in parentheses." The colon in a ? b : c would otherwise start the format part.

Conclusion​

Put $ before a string and write each value in curly braces: $"Hi {name}, you have {count} new messages.". Any expression works inside the braces, {{ and }} print literal braces, and the CS0219 warning is a hint that the $ is missing.

Additional Resources​