String interpolation and formatting help you create text that includes values from variables easily and clearly.
String interpolation and formatting in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
string result = $"Hello, {name}! You have {count} new messages.";Use the $ symbol before the string to start interpolation.
Place variables or expressions inside curly braces { } where you want their values to appear.
string name = "Alice"; int age = 30; string message = $"Name: {name}, Age: {age}";
:C.double price = 12.5; string formatted = $"Price: {price:C}";
DateTime today = DateTime.Now;
string date = $"Today is {today:MMMM dd, yyyy}";int x = 5, y = 10; string sum = $"Sum of {x} and {y} is {x + y}";
This program shows how to use string interpolation to include variables and format numbers and dates in a friendly message.
using System; class Program { static void Main() { string user = "Bob"; int messages = 3; double balance = 45.678; DateTime now = DateTime.Now; string output = $"Hello, {user}! You have {messages} new messages.\n" + $"Your balance is {balance:C2}.\n" + $"Today is {now:dddd, MMMM dd, yyyy}."; Console.WriteLine(output); } }
String interpolation was introduced in C# 6.0 and is now the easiest way to build strings with variables.
You can add formatting inside the braces after a colon, like {value:C2} for currency with 2 decimals.
Remember to use \n for new lines inside strings.
Use $"...{variable}..." to insert variables directly into strings.
You can format numbers and dates inside the braces with a colon and format code.
String interpolation makes your code easier to read and write compared to older methods.
Practice
int age = 25;
string message = $"I am {age} years old.";Solution
Step 1: Understand string interpolation syntax
The dollar sign$before the string allows inserting variables inside curly braces.Step 2: Identify variable insertion
The variableageis inserted where{age}appears, replacing the placeholder with its value.Final Answer:
Inserts the value ofageinto the string at the placeholder. -> Option BQuick Check:
String interpolation = Insert variable value [OK]
- Thinking $ means string concatenation
- Expecting curly braces to print literally
- Confusing interpolation with format method
Solution
Step 1: Recognize C# format specifier syntax
In C#, inside interpolation braces,:0.00formats numbers to two decimals.Step 2: Check each option's correctness
double price = 9.99; string s = $"Price: {price:0.00}"; uses correct format{price:0.00}. double price = 9.99; string s = $"Price: {price,2}"; uses comma which is for alignment, not decimals. double price = 9.99; string s = $"Price: {price:.2f}"; uses Python style.2fwhich is invalid in C#. double price = 9.99; string s = $"Price: {price.ToString("0.00")}"; callsToStringinside interpolation but with escaped quotes incorrectly.Final Answer:
double price = 9.99; string s = $"Price: {price:0.00}"; -> Option AQuick Check:
Format decimals with :0.00 inside {} [OK]
- Using Python or other language format codes
- Confusing alignment comma with format colon
- Trying to call methods inside interpolation incorrectly
int x = 5;
int y = 3;
string result = $"Sum: {x + y}, Product: {x * y}";
Console.WriteLine(result);
Solution
Step 1: Evaluate expressions inside interpolation
The expressions{x + y}and{x * y}calculate 5 + 3 = 8 and 5 * 3 = 15 respectively.Step 2: Check output formatting
The string inserts these values with a comma and space exactly as written, so output is "Sum: 8, Product: 15".Final Answer:
Sum: 8, Product: 15 -> Option CQuick Check:
Expressions inside {} are evaluated before output [OK]
- Thinking expressions print literally
- Concatenating numbers as strings
- Missing commas or spaces in output
int count = 10;
string message = $"Count is {count,2.0}";
Console.WriteLine(message);
Solution
Step 1: Understand alignment and format syntax
In interpolation,{variable,alignment:format}is correct. Here,{count,2.0}mixes alignment and format incorrectly without colon.Step 2: Identify correct syntax
It should be{count,2:0}or similar. The dot without colon causes syntax error.Final Answer:
Incorrect format specifier; cannot combine alignment and decimal format like that. -> Option DQuick Check:
Use comma for alignment, colon for format separately [OK]
- Mixing alignment and format without colon
- Forgetting dollar sign for interpolation
- Assuming no error when syntax is wrong
DateTime object date to achieve this?Solution
Step 1: Understand date format strings in interpolation
Inside interpolation,{date:format}applies the format string to the DateTime object.Step 2: Compare options
DateTime date = new DateTime(2024, 6, 15); string s = $"Year: {date:yyyy}, Month: {date:MM}, Day: {date:dd}"; uses correct format codesyyyy,MM,ddinside interpolation. DateTime date = new DateTime(2024, 6, 15); string s = $"Year: {date.Year}, Month: {date.Month}, Day: {date.Day}"; uses properties but month/day will not have leading zeros. DateTime date = new DateTime(2024, 6, 15); string s = $"Year: {date:Year}, Month: {date:Month}, Day: {date:Day}"; uses invalid format names. DateTime date = new DateTime(2024, 6, 15); string s = $"Year: {date.ToString("yyyy")}, Month: {date.ToString("MM")}, Day: {date.ToString("dd")}"; callsToStringwith escaped quotes incorrectly.Final Answer:
DateTime date = new DateTime(2024, 6, 15); string s = $"Year: {date:yyyy}, Month: {date:MM}, Day: {date:dd}"; -> Option AQuick Check:
Use :format inside {} for DateTime formatting [OK]
- Using property names as format strings
- Forgetting leading zeros for month/day
- Overusing ToString inside interpolation unnecessarily
