Summary#
$"…{expr}…" builds a string from literal text and embedded expressions. A hole may carry a .NET format
specifier after a colon — {amount:F2} — applied via IFormattable in the current culture, exactly like
C#'s string.Format.
Signature#
$"literal {expr} more {expr:format} text"Description#
- Each
{expr}hole is converted to its string form and concatenated with the surrounding literal text. - A
:formatafter the expression applies a .NET format string to a formattable value (numbers, dates):{total:F2}→ two decimals,{n:N0}→ thousands separators,{ratio:P1}→ a percent,{when:yyyy-MM-dd}→ a date. A hole with no specifier writes the value as itsToString()does. - Every hole formats in the CURRENT culture, as C#'s do: the viewer's
[PreferredCulture], elseapp.DefaultCulture, else the invariant culture — never the host machine's, so the text is the same on every server and in every browser. Underapp.DefaultCulture = "sv-SE",$"{1234.5m}"is1234,5. Writestring.Format(CultureInfo.InvariantCulture, "{0}", x)for text a machine reads (Culture formatting — ToString(format, culture)). - A non-formattable value (a
string) ignores the specifier and coerces as usual — matching C#'sstring.Format. - The formatted conversion is also available directly as the 2-arg
Convert.ToString(value, "F2").
Which other interpolated forms are there?#
All of C#'s forms: $@"…" (or @$"…") keeps backslashes as written and may span lines; $"""…""" is a raw
interpolated string (no escapes, quotes written plainly); {x,5} right-aligns the formatted value in five characters
and {x,-5} left-aligns it, combining with a format as {x,6:D4}. A hole may break across lines.
string Row(int qty, string sku) => $@"C:\stock\{sku}" + $""" "{sku}" """ + $"[{qty,5}][{qty,-5}][{qty,6:D4}]";Examples#
string Money(decimal amount) { return $"Total: {amount:F2}"; } // "Total: 1234.50"
string Thousands(int n) { return $"{n:N0}"; } // "1,234,567"
string Percent(decimal ratio) { return $"{ratio:P1}"; } // "12.3 %"
string Plain(int n) { return $"n = {n}"; } // "n = 5" (no specifier)
string Direct(decimal d) { return Convert.ToString(d, "F3"); } // "3.142"See also#
- Numeric types & literal suffixes — the numeric values you format
- Convert —
Convert.ToStringand the coercion builtins