📖 Scenario: You are creating a simple program to greet users by their names and ages. This is like writing a friendly note that includes their details.
🎯 Goal: Build a program that uses string interpolation to create a greeting message with a user's name and age.
📋 What You'll Learn
Create variables for the user's name and age with exact values
Create a greeting message using string interpolation
Print the greeting message
💡 Why This Matters
🌍 Real World
String interpolation is used in many programs to create messages that include changing information, like names, dates, or numbers.
💼 Career
Knowing string interpolation helps you write clear and readable code for user interfaces, reports, and logs in software development jobs.
Progress0 / 4 steps
1
DATA SETUP: Create user variables
Create a string variable called userName and set it to "Alice". Create an integer variable called userAge and set it to 30.
C Sharp (C#)
Hint
Use string for the name and int for the age. Assign the exact values given.
2
CONFIGURATION: Prepare greeting message variable
Create a string variable called greeting but do not assign a value yet.
C Sharp (C#)
Hint
Just declare the variable greeting as a string without assigning a value.
3
CORE LOGIC: Use string interpolation to create greeting
Assign to greeting a string using string interpolation that says: Hello, my name is {userName} and I am {userAge} years old.
C Sharp (C#)
Hint
Use the $"..." syntax and include the variables inside curly braces.
4
OUTPUT: Print the greeting message
Write a Console.WriteLine statement to print the greeting variable.
C Sharp (C#)
Hint
Use Console.WriteLine(greeting); to show the message on the screen.
Practice
(1/5)
1. What does string interpolation in C# allow you to do?
easy
A. Use variables only outside of strings
B. Insert variables directly inside a string using { } with a $ prefix
C. Create strings without any variables
D. Concatenate strings using the + operator only
Solution
Step 1: Understand string interpolation syntax
String interpolation uses a $ before the string and curly braces { } to insert variables inside the string.
Step 2: Compare options with this syntax
Only Insert variables directly inside a string using { } with a $ prefix describes inserting variables inside strings using { } with a $ prefix.
Final Answer:
Insert variables directly inside a string using { } with a $ prefix -> Option B
Quick Check:
String interpolation = $ + {variable} [OK]
Hint: Look for $ and { } to spot string interpolation [OK]
Common Mistakes:
Confusing concatenation (+) with interpolation
Forgetting the $ before the string
Using variables outside the string only
2. Which of the following is the correct syntax for string interpolation in C#?
In C#, string interpolation requires a $ before a double-quoted string and variables inside { }.
Step 2: Check each option
string message = $"Hello, {name}!"; uses $ before double quotes and {name} inside, which is correct. The remaining options lack $ or use single quotes which is invalid for interpolation.
Final Answer:
string message = $"Hello, {name}!"; -> Option A
Quick Check:
$ + "string" + {variable} = correct syntax [OK]
Hint: Use $ before double quotes for interpolation [OK]
Common Mistakes:
Using single quotes instead of double quotes
Omitting the $ symbol
Placing variables outside { }
3. What is the output of this C# code?
int apples = 5; int oranges = 3; string result = $"I have {apples + oranges} fruits."; Console.WriteLine(result);
medium
A. I have 8 fruits.
B. I have {apples + oranges} fruits.
C. I have apples + oranges fruits.
D. I have 53 fruits.
Solution
Step 1: Evaluate the expression inside the interpolation
The expression {apples + oranges} adds 5 + 3, which equals 8.
Step 2: Substitute the result into the string
The string becomes "I have 8 fruits." and is printed.
Final Answer:
I have 8 fruits. -> Option A
Quick Check:
Expression inside { } is calculated before output [OK]
Hint: Calculate expressions inside { } before printing [OK]
Common Mistakes:
Printing the expression text instead of its value
Concatenating numbers as strings incorrectly
Ignoring the + operator inside { }
4. Identify the error in this code snippet:
int age = 30; string message = $"Your age is {age"; Console.WriteLine(message);
medium
A. age variable is not declared
B. Missing $ before the string
C. Missing closing brace } in the interpolation
D. Console.WriteLine is misspelled
Solution
Step 1: Check the interpolation braces
The string has an opening { but no closing } after age, causing a syntax error.
Step 2: Verify other parts
The $ is present, age is declared, and Console.WriteLine is correct.
Final Answer:
Missing closing brace } in the interpolation -> Option C
Quick Check:
Every { must have a matching } [OK]
Hint: Count { and } to avoid missing braces [OK]
Common Mistakes:
Forgetting to close interpolation braces
Removing $ accidentally
Assuming variable is undeclared without checking
5. You want to display a price with two decimal places using string interpolation. Which code correctly formats the price variable?
double price = 12.5; // Fill in the blank: string output = $"Price: {___}"; Console.WriteLine(output);
Inside { }, you can add :F2 to format a number with 2 decimals.
Step 2: Check each option
price:F2 uses {price:F2} which is correct. price:.2 is an invalid format specifier (requires :F2 for fixed decimals). price.ToFixed(2) uses ToFixed which is JavaScript, not C#. "{price:2}" is invalid syntax.
Final Answer:
price:F2 -> Option D
Quick Check:
Use :F2 inside { } to format decimals [OK]
Hint: Use :F2 inside { } to show 2 decimals [OK]
Common Mistakes:
Using JavaScript methods like ToFixed in C#
Trying to format outside the interpolation braces
Incorrect format string syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.