String interpolation

C# 6.0 NETFx 4.6

Published Updated Author Jeffrey T. Fritz Reading time

Use string interpolation ($"...") to build readable strings without manual concatenation.

String interpolation was introduced in C# 6.0. It lets you embed expressions directly in a string with {...} placeholders, which is usually clearer than + concatenation or long string.Format calls.

Why it matters

  • Interpolation keeps the output template and values in one place.
  • It improves readability for logs, messages, and diagnostics.
  • Expressions inside {...} are compile-time checked.

Baseline usage

Use $ before the string and place expressions in braces:

var name = "Sam";
var count = 3;
var message = $"{name} has {count} new notifications.";

You can also format values inline:

var total = 12.5m;
var line = $"Total: {total:C}";

Cautions

  • Escape literal braces with {{ and }}.
  • Keep interpolated expressions simple; move complex logic outside the string.
  • For localization-heavy UI text, prefer resource files over hard-coded interpolated strings.

From string.Format to interpolation

C# 6.0 interpolation replaces positional placeholders with inline expressions for clearer templates.

Valid since C# 6.0

Without var

var userName = "Avery";
var orderTotal = 27.5m;

var message = string.Format(
    "Customer {0} placed an order totaling {1:C}.",
    userName,
    orderTotal
);

With var

var userName = "Avery";
var orderTotal = 27.5m;

var message = $"Customer {userName} placed an order totaling {orderTotal:C}.";

Formatting and alignment in interpolated strings

Interpolation supports the same numeric/date format and alignment components used with composite formatting.

Valid since C# 6.0

var serviceName = "Billing";
var elapsedMs = 17.234;
var startedAt = new DateTime(2026, 06, 17, 11, 19, 0, DateTimeKind.Utc);

var logLine = $"{serviceName,-12} | {elapsedMs,8:F2} ms | {startedAt:O}";

Learn more

String interpolation (Microsoft Learn)

Newer capabilities

  1. Interpolated string handlers and raw interpolated strings

    Introduced in C# 10.0

    Optional newer-version capability (C# 10+ / C# 11+)

    The baseline guidance for this article is C# 6.0.

    • C# 10+: custom interpolated string handlers can reduce allocations in high-performance scenarios.
    • C# 11+: interpolated raw string literals ($"""...""") make multi-line templates and embedded quotes easier to write.