How does string interpolation work, and is it slower than concatenation?
Quick Answer
$"Hello {name}" is compiled to a string.Format / string.Concat call, or since C# 10 to an efficient interpolated string handler that appends directly to a buffer. For a few values it's about the same as concatenation; in a loop, use a StringBuilder regardless.
String interpolation with the $ prefix gets compiled to a format or concat call, and since C# 10 to an interpolated string handler that appends straight into a buffer without building intermediate strings. For a handful of values the performance is basically the same as +. In a loop you still want a StringBuilder. Two nice tricks: assigning to FormattableString defers the formatting, which EF Core uses to safely parameterise interpolated SQL, and the handler makes LogDebug($"...") skip building the string entirely when Debug logging is off.
- Assign to
FormattableStringto defer formatting (useful for culture control or SQL —FromSqlInterpolatedparameterises safely). - Interpolated string handlers power
StringBuilder.Append($"...")with no intermediate string, and conditional logging (LogDebug($"...")skips building the string if Debug is off). - Don't use interpolation in structured logging message templates — it defeats the named-property capture.