Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

String interpolation

Reference for C# string interpolation: the $ prefix, format specifiers, alignment, raw and verbatim forms, nested quotes and custom interpolated string handlers.

An interpolated string starts with $ before the opening quote. Inside it, anything in curly braces is a C# expression whose value is converted to text and placed in the result. It is the shortest and most readable way to build a message from values.

The compiler turns an interpolated string into a call to string.Format, a DefaultInterpolatedStringHandler, or a constant, depending on how you use it. You rarely need to think about which.

C#
string name = "Ana";
string text = $"Hello, {name}! You are {age + 1} next year.";

Try it

C# Format specifiers and alignment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Verbatim, raw and multi-line forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
$"text {expression}"
$"text {expression,alignment}"
$"text {expression:format}"
$"text {expression,alignment:format}"
$@"verbatim {expression}"
$$"""raw {{expression}}"""
Name Description
expression Any expression. Its value is formatted with ToString or the formatting interfaces.
alignment A constant integer. Positive pads on the left (right-aligns), negative pads on the right.
format A format string such as F2, N0, X, yyyy-MM-dd. It starts after a colon and ends at the closing brace.

Returns: a string, or a FormattableString or IFormattable when the target type asks for one.

Errors: a malformed hole is a compile error. A conditional expression with a bare colon must be parenthesized, because : starts the format string.

Usage notes

  • Culture matters. A plain $"{price}" uses the current culture, which is the invariant culture on our runner. In your own program a German culture prints 1234,5678. Use string.Create(CultureInfo.InvariantCulture, $"...") when the text must be machine readable.
  • {{ and }} produce literal braces. In a $$ raw string two braces open a hole and a single brace is literal text, which suits JSON.
  • Put the conditional operator in parentheses: {(ok ? "yes" : "no")}. Without them the colon is read as a format specifier.
  • Since C# 11 a hole may contain string literals with quotes and line breaks, as the examples show. Older compilers need a helper variable for that.
  • When the target is FormattableString, string.Create or a method that takes an interpolated string handler (such as logging APIs), the compiler can avoid building a string at all.
  • Logging: logger.LogInformation($"User {id}") defeats structured logging. Use the template form "User {Id}" with arguments.
  • Do not use $ strings for SQL: $"... WHERE id = {id}" is an injection risk unless the API takes FormattableString and parameterizes.
  • A call string.Format("{0}", x) is equivalent but harder to read and keep in sync with its arguments.
  • Interpolated strings are constants only if every hole is a constant string (C# 10).