String Interpolation · TL;DR
1 min readTL;DR
An interpolated string literal is a string token prefixed with $ whose {…} holes contain C# expressions: $"Hello {name}, you owe {amount:C2}". The compiler decides what code to emit from the target type: assigned to string, C# 10+ builds it with DefaultInterpolatedStringHandler (generic AppendFormatted<T>, no boxing, pooled buffer); assigned to FormattableString or IFormattable, it keeps the format and the arguments separately, which is how EF Core's FromSql($"… {id}") becomes a parameterized query; passed to an API with a custom handler (StringBuilder.Append, Debug.Assert, span.TryWrite), it writes directly into that API's buffer, possibly skipping formatting altogether. Holes accept alignment and format ({x,10:F2}) and use the current culture unless you say otherwise. Variants: verbatim $@"…", raw $"""…""" (C# 11) with $$ for literal braces, constant interpolated strings (C# 10), and newlines inside holes (C# 11). Traps: culture-dependent output, SQL injection through FromSqlRaw($"…"), and interpolation in ILogger calls.