Coding/Learn/csharp/string interpolation
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnString Interpolation & Modern Formatting
5 / 20 0%
Lesson 05 of 20
5 mins read

String Interpolation & Modern Formatting

PreviousNext Lesson
In Plain English

Old-school string concatenation ("Hello " + name + " you have " + count + " items") is messy and error-prone. Modern C# provides string interpolation ($"...{expression}...") which lets you embed variables and math directly inside strings.

The Mental Model

"String concatenation is like taping separate words onto a banner with Scotch tape. String interpolation ($) is like a smart fill-in-the-blank Mad Libs card that automatically plugs in the answers seamlessly."

Deep Dive: How It Works

The $ Prefix: Placing $ before double quotes unlocks { } expression placeholders: $"Hello, {userName}!".

Inline Expressions: You can execute math inside the brackets: $"Next year you will be {age + 1} years old.".

Format Specifiers: Format numbers directly: {price:C} (Currency: $19.99), {score:N0} (Comma separated: 1,000,000), {ratio:P1} (Percentage: 85.5%).

Verbatim Strings (@): @"C:\Users\Neo\Documents" ignores escape slashes.

Syntax Blueprint

string user = "Alex";
int level = 5;
Console.WriteLine($"Player {user} is Level {level}!");

Prepend $ to string and place variables/expressions inside curly braces { }.

PointCore Rules to Remember

Point star
The $ Symbol: Must be placed directly before the opening quotation mark.
Point star
Cleaner Code: Far more readable and maintainable than chaining dozens of + signs.
Point star
Built-in Formatting: Supports formatting tokens like {price:F2} for 2-decimal rounding.

Common Beginner Traps & How to Fix Them

Mistake:Putting the $ inside the quotes: "{user}"

Why it happens: Forgetting the $ goes outside the string literal.

How to fix: Write $"Hello {user}".

Pro Tip: In C# 11+, you can use raw string literals with triple quotes (""" ... """) to write multi-line JSON or SQL without escaping anything!

Live Interactive Example

Hit Run Code to see it live
String Interpolation in Action
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: String interpolation cleanly formats numbers with commas and percentages.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Use string interpolation

  • PointDeclare string product = "Laptop"; and double price = 899.50;
  • PointPrint "Item: Laptop costs $899.50" using string interpolation ($).
1
2
3
4
5
6
7
8
9
10
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates