TExcelTAD
Home › C# › C# String Interpolation

C# String Interpolation

String interpolation lets you embed expressions directly inside a string literal. Put a $ in front of the opening quote and write any C# expression inside curly braces. It is the clearest way to build messages, and it is what most modern C# code uses.

csharp · try it yourself
using System;

string product = "Keyboard";
int quantity = 3;
decimal unitPrice = 24.50m;

Console.WriteLine(
quot;{quantity} x {product} = {quantity * unitPrice}"); Console.WriteLine(
quot;Uppercase: {product.ToUpper()}, letters: {product.Length}");
3 x Keyboard = 73.50
Uppercase: KEYBOARD, letters: 8

Compare that with concatenation: quantity + " x " + product + " = " + (quantity * unitPrice). The interpolated version reads like the final output.

Format specifiers

After the expression, a colon introduces a format string that controls how the value is displayed.

Specifier Meaning Example Output
F2 Fixed-point, 2 decimals {3.14159:F2} 3.14
N0 / N2 Thousands separators {1234567.891:N2} 1,234,567.89
P1 Percentage {0.256:P1} 25.6 %
D5 Integer padded with zeros {42:D5} 00042
X Hexadecimal {255:X} FF
E2 Scientific notation {12345.0:E2} 1.23E+004
custom Digit pattern {7.5:0.00} 7.50
csharp · try it yourself
using System;

double ratio = 0.8642;
int orderId = 315;
double distance = 1234567.891;

Console.WriteLine(
quot;Ratio: {ratio:F2}"); Console.WriteLine(
quot;Order #{orderId:D6}"); Console.WriteLine(
quot;Distance: {distance:N1} km"); Console.WriteLine(
quot;Hex: {orderId:X}"); Console.WriteLine(
quot;Custom: {ratio:0.0#}");
Ratio: 0.86
Order #000315
Distance: 1,234,567.9 km
Hex: 13B
Custom: 0.86

Note: Number and date formats depend on the current culture (decimal comma vs. point, the currency symbol and so on). Our playground runs with invariant settings, so your local machine may show slightly different separators.

Alignment

A comma followed by a width pads the value to that many characters. Positive widths right-align, negative widths left-align — perfect for simple tables. Width and format can be combined: {value,8:F2}.

csharp · try it yourself
using System;

string[] items = { "Coffee", "Bagel", "Orange juice" };
double[] prices = { 3.5, 2.25, 4.0 };

Console.WriteLine(
quot;{"Item",-14}|{"Price",8}"); Console.WriteLine(new string('-', 23)); for (int i = 0; i < items.Length; i++) { Console.WriteLine(
quot;{items[i],-14}|{prices[i],8:F2}"); }
Item          |   Price
-----------------------
Coffee        |    3.50
Bagel         |    2.25
Orange juice  |    4.00

Braces, conditionals and multi-line strings

  • To print a literal brace, double it: {{ and }}.
  • A conditional expression must be wrapped in parentheses because of the colon: {(x > 0 ? "pos" : "neg")}.
  • Combine $ with @ for a verbatim interpolated string, or use a raw string literal (""") for multi-line text.
csharp · try it yourself
using System;

int score = 72;
Console.WriteLine(
quot;{{score}} = {score}"); Console.WriteLine(
quot;Result: {(score >= 50 ? "pass" : "fail")}"); string name = "Lin"; string folder = $@"C:\Users\{name}\Documents"; Console.WriteLine(folder); string json = $""" { "name": "{{name}}", "score": {{score}} } """; Console.WriteLine(json);
{score} = 72
Result: pass
C:\Users\Lin\Documents
{ "name": "Lin", "score": 72 }

In the raw string $"""…""", two dollar signs mean that interpolation holes use double braces {{ }}, so single braces — common in JSON — are printed as-is.

Test yourself

What does

quot;{5.678:F1}" produce?

  1. 5.6
  2. 5.7
  3. 5.678
  4. 6

How do you print a literal { inside an interpolated string?

  1. \{
  2. {{
  3. ${
  4. It is not possible

What does the -10 mean in {name,-10}?

  1. Take the last 10 characters
  2. Left-align in a field 10 characters wide
  3. Subtract 10
  4. Right-align in a field 10 characters wide