Introduction
String interpolation helps you easily put values inside text without extra steps.
String interpolation helps you easily put values inside text without extra steps.
string message = $"Hello, {name}! You have {count} new messages.";string name = "Anna"; string greeting = $"Hi, {name}!";
int apples = 5; string message = $"You have {apples} apples.";
int a = 3, b = 4; string result = $"Sum is {a + b}.";
DateTime today = DateTime.Now;
string date = $"Today is {today:MMMM dd, yyyy}.";This program greets the user by name and tells how many new messages they have using string interpolation.
using System; class Program { static void Main() { string name = "Sam"; int messages = 3; string output = $"Hello, {name}! You have {messages} new messages."; Console.WriteLine(output); } }
You can put any expression inside the braces, not just variables.
Use @ before $ for multi-line interpolated strings: @$"..."
Be careful with curly braces if you want to show them literally: use double braces {{ or }}.
String interpolation makes combining text and values easy and clear.
Use $ before the string and put variables inside { }.
It works with variables, expressions, and formatting.
int apples = 5;
int oranges = 3;
string result = $"I have {apples + oranges} fruits.";
Console.WriteLine(result);
int age = 30;
string message = $"Your age is {age";
Console.WriteLine(message);
double price = 12.5;
// Fill in the blank:
string output = $"Price: {___}";
Console.WriteLine(output);
Pass the test and this pattern moves unknown