How to use string interpolation in C#
String interpolation in C# uses the $ prefix before a string literal, allowing you to embed expressions directly inside curly braces. This is more readable than string.Format() or concatenation.
You can include format specifiers after a colon inside the braces, like {value:C} for currency or {number:F2} for two decimal places.
Interpolated strings are evaluated at compile time and converted to string.Format() calls, making them both efficient and type-safe.
using System;
string name = "Alice";
int age = 30;
// Basic string interpolation
string message = $"My name is {name} and I am {age} years old.";
Console.WriteLine(message);
// With expressions
int a = 10, b = 20;
Console.WriteLine($"Sum: {a + b}, Product: {a * b}");
// Formatting numbers
double price = 49.99;
Console.WriteLine($"Price: {price:C}"); // Currency format
Console.WriteLine($"Price: ${price:F2}"); // Two decimal places
// Formatting dates
DateTime now = DateTime.Now;
Console.WriteLine($"Date: {now:yyyy-MM-dd}");
Console.WriteLine($"Time: {now:HH:mm:ss}");
// Method calls in interpolation
string text = "hello";
Console.WriteLine($"Uppercase: {text.ToUpper()}");
// Alignment and padding
string header1 = "Name";
string header2 = "Age";
Console.WriteLine($"{header1,-10} {header2,5}");
Console.WriteLine($"{name,-10} {age,5}");
// Verbatim interpolated strings
string path = $@"C:\Users\{name}\Documents";
Console.WriteLine($"Path: {path}");
// Conditional expressions
int score = 85;
Console.WriteLine($"Result: {(score >= 60 ? "Pass" : "Fail")}");Comparing with string.Format
String interpolation is a shorter, more readable way to write string.Format. The two are functionally equivalent.
| string.Format | String interpolation |
|---|---|
string.Format("{0}", name) | $"{name}" |
string.Format("{0:F2}", price) | $"{price:F2}" |
string.Format("{0,-10}", name) | $"{name,-10}" |
string.Format("{0:yyyy-MM-dd}", dt) | $"{dt:yyyy-MM-dd}" |
string.Format("{0} + {1} = {2}", a, b, a+b) | $"{a} + {b} = {a+b}" |
string name = "Alice";
double price = 49.99;
DateTime dt = new DateTime(2024, 3, 15);
// string.Format
string s1 = string.Format("Name: {0}, Price: {1:C}, Date: {2:yyyy-MM-dd}", name, price, dt);
// Equivalent interpolation — same result, more readable
string s2 = $"Name: {name}, Price: {price:C}, Date: {dt:yyyy-MM-dd}";
Console.WriteLine(s1 == s2); // TruePerformance: When to Use StringBuilder Instead
For a small number of fixed parts, $"" is perfectly fine. For building strings inside loops or combining a large number of values, use StringBuilder to avoid creating a new string object on every iteration.
// Fine — a few fixed parts
string message = $"Hello, {name}! You have {count} messages.";
// Slow — each iteration allocates a new string
string result = "";
for (int i = 0; i < 1000; i++)
result += $"Item {i}, "; // 1000 intermediate strings discarded
// Fast — StringBuilder accumulates without intermediate allocations
var sb = new System.Text.StringBuilder();
for (int i = 0; i < 1000; i++)
sb.Append($"Item {i}, ");
string efficient = sb.ToString();Take It Further

The community's favorite C# guide — teaches every concept you just read about with clarity.
The C# Player's Guide — 5th Edition · RB Whitaker
Get it on Amazon →As an Amazon Associate I earn from qualifying purchases.