String Interpolation Basics
Embed expressions directly inside strings.
Interpolated Strings
An interpolated string starts with a dollar sign before the opening quote. Inside, you place expressions in curly braces, and C# inserts their values.
using System;
class Program {
static void Main() {
string name = "Ada";
Console.WriteLine($"Hello, {name}!");
}
}Embedding Expressions
The braces can hold any expression, not just a variable. C# evaluates it and converts the result to text.
using System;
class Program {
static void Main() {
int a = 3, b = 4;
Console.WriteLine($"Sum is {a + b}");
}
}All lessons in this course
- String Interpolation Basics
- Composite Formatting with string.Format
- Numeric and Date Format Specifiers
- Raw String Literals