Bird
Raised Fist0

Identify the error in this C# code snippet:

medium📝 Debug Q14 of Q15
C Sharp (C#) - Strings and StringBuilder
Identify the error in this C# code snippet:
int count = 10;
string message = $"Count is {count,2.0}";
Console.WriteLine(message);
AVariable <code>count</code> is not declared.
BMissing dollar sign for string interpolation.
CNo error; code runs fine.
DIncorrect format specifier; cannot combine alignment and decimal format like that.
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Identify correct syntax

    It should be {count,2:0} or similar. The dot without colon causes syntax error.
  3. Final Answer:

    Incorrect format specifier; cannot combine alignment and decimal format like that. -> Option D
  4. Quick Check:

    Use comma for alignment, colon for format separately [OK]
Quick Trick: Use comma for alignment, colon for format inside {} [OK]
Common Mistakes:
MISTAKES
  • Mixing alignment and format without colon
  • Forgetting dollar sign for interpolation
  • Assuming no error when syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes