📖 Scenario: You are creating a simple program to display information about a product in a store. You want to show the product's name, price, and quantity in a neat sentence.
🎯 Goal: Build a C# program that uses composite formatting to display product details in a formatted string.
📋 What You'll Learn
Create variables for product name, price, and quantity with exact values.
Create a format string variable using composite formatting placeholders.
Use string.Format with the format string and variables to create the final message.
Print the final formatted message to the console.
💡 Why This Matters
🌍 Real World
Composite formatting is used in many programs to create readable messages by inserting variable values into strings, such as receipts, reports, or user messages.
💼 Career
Understanding composite formatting helps you display data clearly in applications, which is a common task in software development jobs.
Progress0 / 4 steps
1
Create product variables
Create a string variable called productName and set it to "Laptop". Create a double variable called price and set it to 999.99. Create an int variable called quantity and set it to 5.
C Sharp (C#)
Hint
Use string for text, double for decimal numbers, and int for whole numbers.
2
Create the format string
Create a string variable called formatString and set it to "Product: {0}, Price: ${1}, Quantity: {2}". This string uses composite formatting placeholders.
C Sharp (C#)
Hint
Use curly braces with numbers inside to mark where variables will go.
3
Use string.Format with variables
Create a string variable called message and set it to the result of string.Format(formatString, productName, price, quantity).
C Sharp (C#)
Hint
Use string.Format with the format string and variables in order.
4
Print the formatted message
Write Console.WriteLine(message); to display the formatted product details on the screen.
C Sharp (C#)
Hint
Use Console.WriteLine to print the message variable.
Practice
(1/5)
1. What does composite formatting in C# allow you to do?
easy
A. Create new data types
B. Mix text and variable values in one string easily
C. Write comments inside strings
D. Declare variables without types
Solution
Step 1: Understand composite formatting purpose
Composite formatting lets you combine text and values in one string using placeholders.
Step 2: Identify correct description
Only Mix text and variable values in one string easily describes mixing text and values easily, which is the main use of composite formatting.
Final Answer:
Mix text and variable values in one string easily -> Option B
Quick Check:
Composite formatting = mix text and values [OK]
Hint: Remember: curly braces hold values inside strings [OK]
Common Mistakes:
Thinking composite formatting creates new types
Confusing comments with formatting
Assuming it changes variable declarations
2. Which of the following is the correct syntax for composite formatting in C#?
easy
A. Console.WriteLine("Hello {0}", name);
B. Console.WriteLine("Hello %0", name);
C. Console.WriteLine("Hello $0", name);
D. Console.WriteLine("Hello #0", name);
Solution
Step 1: Recall composite formatting syntax
Composite formatting uses curly braces with an index number like {0} to insert values.
Step 2: Match syntax to options
Only Console.WriteLine("Hello {0}", name); uses curly braces and index correctly: "Hello {0}".
Final Answer:
Console.WriteLine("Hello {0}", name); -> Option A
Quick Check:
Curly braces + index = correct syntax [OK]
Hint: Look for curly braces with numbers inside [OK]
Common Mistakes:
Using % or $ instead of curly braces
Forgetting the index number inside braces
Placing variables inside the string directly
3. What is the output of this code?
int x = 5; double y = 3.14159; Console.WriteLine("X = {0}, Y = {1:F2}", x, y);
medium
A. X = 5, Y = 3.1
B. X = 5, Y = 3.14159
C. X = {0}, Y = {1:F2}
D. X = 5, Y = 3.14
Solution
Step 1: Understand format specifier {1:F2}
The specifier F2 formats the double to 2 decimal places, so 3.14159 becomes 3.14.
Step 2: Substitute values into placeholders
{0} is replaced by 5, {1:F2} by 3.14, so output is "X = 5, Y = 3.14".
Final Answer:
X = 5, Y = 3.14 -> Option D
Quick Check:
Format specifier F2 rounds to 2 decimals [OK]
Hint: F2 means 2 decimals for numbers [OK]
Common Mistakes:
Ignoring format specifier and printing full double
Printing placeholders literally
Rounding incorrectly to 1 decimal
4. Identify the error in this composite formatting code:
int a = 10; int b = 20; Console.WriteLine("Values: {0} and {2}", a, b);
medium
A. Index 2 is out of range for arguments
B. Missing semicolon after WriteLine
C. Variables a and b are not declared
D. Curly braces should be square brackets
Solution
Step 1: Check placeholders and arguments count
Placeholders {0} and {2} are used, but only two arguments (a and b) exist at indexes 0 and 1.
Step 2: Identify invalid index
Index 2 does not exist in arguments, causing an error.
Final Answer:
Index 2 is out of range for arguments -> Option A
Quick Check:
Placeholder index must match argument count [OK]
Hint: Check if placeholder indexes match argument count [OK]
Common Mistakes:
Using placeholder index beyond arguments
Confusing syntax errors with formatting errors
Assuming curly braces can be replaced
5. You want to format a date and a number in one string like this: "Date: 2024-06-15, Price: $123.46" Which composite formatting code produces this output?
DateTime d = new DateTime(2024, 6, 15); double price = 123.4567; Console.WriteLine(???);
hard
A. "Date: {0}, Price: {1}", d.ToShortDateString(), price
B. "Date: {0:MM/dd/yyyy}, Price: ${1:F2}", d, price
C. "Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price
D. "Date: {0:yyyy-MM-dd}, Price: {1:F2}", d, price
Solution
Step 1: Match date format to output
The output date is "2024-06-15", which matches format "yyyy-MM-dd" used in "Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price and D.
Step 2: Match price format to output
Price is "$123.46" (rounded to 2 decimals, with $ sign). "Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price uses ${1:F2} which prints $ and 2 decimals exactly. D uses {1:F2} which formats to 2 decimals but lacks the $ sign.
Step 3: Choose exact match
"Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price matches the exact output format given.
Final Answer:
"Date: {0:yyyy-MM-dd}, Price: ${1:F2}", d, price -> Option C
Quick Check:
Date format + F2 for decimals = exact output [OK]
Hint: Use yyyy-MM-dd for date and F2 for 2 decimals [OK]
Common Mistakes:
Using wrong date format like MM/dd/yyyy
Using currency format {1:C} without controlling decimals
Not including $ sign explicitly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.