CKTIS / C#
String interpolation
String interpolation inserts expression results directly into text using $"..." syntax. It supports value formatting, alignment, and literal braces; choose a culture explicitly when numeric formatting must be predictable.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
$"text {expression[,width][:formatString]}"In a regular interpolated string literal, $"...", the $ must immediately precede the opening quote. C# also accepts the interpolated verbatim form $@"...", where @ separates $ from the opening quote. Expressions inside {} supply values inserted into the resulting text. Without the $ prefix, braces remain ordinary text. A null value produces empty text.
An interpolation item has the form {expression[,width][:formatString]}; square brackets indicate optional parts. The integer constant width sets a minimum width: positive values align right, negative values align left, without truncating longer values. :F2 formats a number with two decimal places. The value's type must support the format.
In a regular interpolated literal, write {{ and }} to produce literal braces. Ordinary escape sequences such as \n still work. Put a conditional expression using ?: in parentheses because : introduces a format.
Default formatting depends on CultureInfo.CurrentCulture. The example uses string.Create with CultureInfo.InvariantCulture to make the decimal separator and output predictable. It combines literal braces, interpolation of name, and alignment and formatting of price.
Examples
using System;
using System.Globalization;
string name = "Ada";
decimal price = 12.5m;
string message = string.Create(
CultureInfo.InvariantCulture,
$"{{{name}}}: |{price,7:F2}|");
Console.WriteLine(message);Result
{Ada}: | 12.50|The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.