String Interpolation Basics
Embed expressions directly inside strings.
String Interpolation Basics is a free C# Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
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}");
}
}Calling Methods Inside
You can call methods or properties inside the braces, keeping output code compact and readable.
using System;
class Program {
static void Main() {
string word = "hello";
Console.WriteLine($"Upper: {word.ToUpper()}");
}
}Interpolation vs Concatenation
Interpolation is usually clearer than chaining many plus signs, especially with several variables.
using System;
class Program {
static void Main() {
string first = "Grace", last = "Hopper";
Console.WriteLine($"{first} {last}");
}
}Multiple Placeholders
You can include as many braces as you need in one string.
using System;
class Program {
static void Main() {
int day = 5, month = 11, year = 2026;
Console.WriteLine($"{day}/{month}/{year}");
}
}Escaping Braces
To print a literal curly brace, double it. Two opening braces produce one in the output.
using System;
class Program {
static void Main() {
int x = 5;
Console.WriteLine($"Value in braces: {{{x}}}");
}
}Format Specifiers
Inside the braces you may add a colon and a format specifier, like a number of decimal places.
using System;
class Program {
static void Main() {
double price = 3.14159;
Console.WriteLine($"Price: {price:F2}");
}
}Alignment in Interpolation
A comma plus a number sets alignment width: positive right-aligns, negative left-aligns within that width.
using System;
class Program {
static void Main() {
Console.WriteLine($"[{"hi",10}]");
Console.WriteLine($"[{"hi",-10}]");
}
}Ternary Inside Interpolation
Conditional expressions work inside the braces, though wrapping them in parentheses keeps the parser happy.
using System;
class Program {
static void Main() {
int n = 7;
Console.WriteLine($"{n} is {(n % 2 == 0 ? "even" : "odd")}");
}
}Building Messages
Interpolation makes user-facing messages easy to read and maintain.
using System;
class Program {
static void Main() {
string user = "Lee";
int items = 3;
Console.WriteLine($"{user}, you have {items} new messages.");
}
}Interpolation Is Compile-Checked
Unlike runtime templates, an interpolated string is checked at compile time, so missing variables are caught early.
using System;
class Program {
static void Main() {
int score = 95;
string grade = score >= 90 ? "A" : "B";
Console.WriteLine($"Score {score} earns grade {grade}");
}
}Quick Check
Test your understanding of string interpolation.
Recap
Interpolated strings begin with a dollar sign and embed expressions in curly braces, which C# evaluates and converts to text. They are clearer than concatenation and are compile-time checked.
You can add format specifiers and alignment after a colon and comma, and you escape literal braces by doubling them.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “String Interpolation Basics” lesson free?
Yes — the full text of “String Interpolation Basics” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “String Interpolation Basics”?
Embed expressions directly inside strings. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “String Interpolation Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String Interpolation Basics
- Composite Formatting with string.Format
- Numeric and Date Format Specifiers
- Raw String Literals