C# String Interpolation - Complete Guide
Introduction to String Interpolation
String interpolation in C# (introduced in C# 6.0) provides a clear and concise way to build strings by embedding expressions directly inside string literals.
It improves readability compared to traditional concatenation or string.Format and reduces the likelihood of formatting mistakes.
Basic String Interpolation
Use the $ symbol before a string literal to enable interpolation. Expressions are placed inside curly braces { }.
Example
using System;
namespace StringInterpolationExample
{
class Program
{
static void Main(string[] args)
{
string name = "Alice";
int age = 25;
double salary = 55000.50;
DateTime now = DateTime.Now;
Console.WriteLine($"Hello, {name}!");
Console.WriteLine($"Name: {name}, Age: {age}, Salary: {salary}");
Console.WriteLine($"Salary: {salary:C}, Date: {now:yyyy-MM-dd}");
Console.WriteLine($"Next year, {name} will be {age + 1} years old");
Console.WriteLine($"Uppercase: {name.ToUpper()}");
Console.WriteLine($"Status: {(age >= 18 ? "Adult" : "Minor")}");
string traditional = string.Format("Hello, {0}! Age: {1}", name, age);
string interpolated = $"Hello, {name}! Age: {age}";
Console.WriteLine($"Traditional: {traditional}");
Console.WriteLine($"Interpolated: {interpolated}");
}
}
}
Output
Hello, Alice! Name: Alice, Age: 25, Salary: 55000.5 Salary: $55,000.50, Date: 2023-10-15 Next year, Alice will be 26 years old Uppercase: ALICE Status: Adult Traditional: Hello, Alice! Age: 25 Interpolated: Hello, Alice! Age: 25
Advanced Interpolation Features
Interpolation supports formatting, alignment, verbatim strings, null-handling, conditional expressions, and culture-specific formats.
Example
using System;
using System.Globalization;
namespace AdvancedInterpolationExample
{
class Program
{
static void Main(string[] args)
{
string[] names = { "Alice", "Bob", "Charlie" };
int[] ages = { 25, 30, 35 };
double[] salaries = { 55000.5, 60000.75, 75000.25 };
Console.WriteLine("Name Age Salary");
Console.WriteLine("-----------------------");
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine($"{names[i],-10} {ages[i],3} {salaries[i],10:C}");
}
double temperature = 23.4567;
Console.WriteLine($"Temperature: {temperature:F2}°C");
string filePath = $"C:\\Users\\{names[0]}\\Documents";
string verbatimPath = $@"C:\Users\{names[0]}\Documents";
Console.WriteLine($"Regular path: {filePath}");
Console.WriteLine($"Verbatim path: {verbatimPath}");
int score = 85;
Console.WriteLine($"Score: {score} ({(score >= 60 ? "PASS" : "FAIL")})");
string nullableName = null;
Console.WriteLine($"Hello, {nullableName ?? "Guest"}!");
CultureInfo french = new CultureInfo("fr-FR");
double amount = 1234.56;
FormattableString formattable = $"Amount: {amount:C}";
Console.WriteLine($"US format: {amount:C}");
Console.WriteLine($"French format: {formattable.ToString(french)}");
}
}
}
Output
Name Age Salary ----------------------- Alice 25 $55,000.50 Bob 30 $60,000.75 Charlie 35 $75,000.25 Temperature: 23.46°C Regular path: C:\Users\Alice\Documents Verbatim path: C:\Users\Alice\Documents Score: 85 (PASS) Hello, Guest! US format: $1,234.56 French format: Amount: 1 234,56 €