Composite formatting helps you create strings by mixing fixed text with variable values easily.
Composite formatting in C Sharp (C#)
string.Format("text {index:format} text", value0, value1, ...);Use curly braces { } to mark where values go inside the string.
Inside the braces, index is the position of the value, and format is optional to control how the value looks.
string result = string.Format("Hello, {0}!", "Alice"); // result is "Hello, Alice!"
string result = string.Format("You have {0} new messages.", 5); // result is "You have 5 new messages."
string result = string.Format("Price: {0:C2}", 12.5); // result is "Price: $12.50" (currency format)
string result = string.Format("Date: {0:yyyy-MM-dd}", DateTime.Now); // result is "Date: 2024-06-01" (example date)
This program creates a message with a name, age, and height formatted to 2 decimal places.
using System; class Program { static void Main() { string name = "Bob"; int age = 30; double height = 1.75; string message = string.Format("Name: {0}, Age: {1}, Height: {2:F2} meters", name, age, height); Console.WriteLine(message); } }
Index numbers start at 0 and match the order of values after the string.
You can use format specifiers like F2 for fixed decimals, C for currency, or date formats.
Composite formatting helps keep your code clean and easy to read.
Composite formatting mixes text and values in one string easily.
Use curly braces with index numbers to place values.
Format specifiers control how values appear, like decimals or dates.
Practice
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 BQuick Check:
Composite formatting = mix text and values [OK]
- Thinking composite formatting creates new types
- Confusing comments with formatting
- Assuming it changes variable declarations
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 AQuick Check:
Curly braces + index = correct syntax [OK]
- Using % or $ instead of curly braces
- Forgetting the index number inside braces
- Placing variables inside the string directly
int x = 5;
double y = 3.14159;
Console.WriteLine("X = {0}, Y = {1:F2}", x, y);
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 DQuick Check:
Format specifier F2 rounds to 2 decimals [OK]
- Ignoring format specifier and printing full double
- Printing placeholders literally
- Rounding incorrectly to 1 decimal
int a = 10;
int b = 20;
Console.WriteLine("Values: {0} and {2}", a, b);
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 AQuick Check:
Placeholder index must match argument count [OK]
- Using placeholder index beyond arguments
- Confusing syntax errors with formatting errors
- Assuming curly braces can be replaced
"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(???);
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 CQuick Check:
Date format + F2 for decimals = exact output [OK]
- Using wrong date format like MM/dd/yyyy
- Using currency format {1:C} without controlling decimals
- Not including $ sign explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
