0
0
C Sharp (C#)programming~5 mins

Composite formatting in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is composite formatting in C#?
Composite formatting is a way to create formatted strings by embedding placeholders like {0}, {1}, etc., in a string, which are replaced by values provided as arguments.
Click to reveal answer
beginner
How do you write a placeholder for the second argument in composite formatting?
You write it as {1} because placeholders start counting from zero, so {0} is the first argument, {1} is the second, and so on.
Click to reveal answer
beginner
What will this code output? <br>
Console.WriteLine("Hello, {0}! Today is {1}.", "Alice", "Monday");
It will output: Hello, Alice! Today is Monday.
Click to reveal answer
intermediate
How can you format a number to show two decimal places using composite formatting?
Use a format specifier inside the placeholder like {0:F2}, where F2 means fixed-point with 2 decimals. Example: <br>
Console.WriteLine("Price: {0:F2}", 3.14159);
outputs "Price: 3.14".
Click to reveal answer
intermediate
What happens if you provide fewer arguments than placeholders in composite formatting?
You get a runtime error (FormatException) because the placeholders expect matching arguments to replace them.
Click to reveal answer
In composite formatting, what does {0} represent?
AThe first argument to replace in the string
BThe second argument to replace in the string
CA literal zero character
DA format specifier
What will this code print? <br>
Console.WriteLine("{1} scored {0} points.", 10, "Bob");
A10 scored Bob points.
BBob scored 10 points.
CBob scored {0} points.
DRuntime error
How do you format a date using composite formatting to show only the year?
A{0:yyyy}
B{0:year}
C{0:Y}
D{0:yy}
What error occurs if placeholders exceed the number of arguments?
ANullReferenceException
BFormatException
CIndexOutOfRangeException
DNo error, placeholders are ignored
Which method uses composite formatting in C#?
AConsole.WriteLine(string, params object[])
BConsole.ReadLine()
Cstring.Concat()
DConsole.Clear()
Explain how composite formatting works in C# and give an example.
Think about how you replace parts of a string with values.
You got /4 concepts.
    What are common mistakes when using composite formatting and how can you avoid them?
    Consider what happens if you forget an argument or use wrong placeholder numbers.
    You got /4 concepts.