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 InvariantCulture, 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. Formatting always usesInvariantCulture(deterministic across hosts). - 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").
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