Bird
0
0

Find the issue in this code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Strings and StringBuilder
Find the issue in this code:
string s = null;
s += "World";
Console.WriteLine(s);
AThrows NullReferenceException
BOutputs nullWorld
COutputs World
DCompile error due to null string
Step-by-Step Solution
Solution:
  1. Step 1: Understand += with null string

    When s is null, s += "World" is equivalent to s = null + "World".
  2. Step 2: Null treated as empty string

    Concatenation treats null as empty string, so s becomes "World".
  3. Final Answer:

    Outputs World -> Option C
  4. Quick Check:

    Null string concatenated acts as empty string [OK]
Quick Trick: Null string plus text results in just the text [OK]
Common Mistakes:
MISTAKES
  • Expecting NullReferenceException
  • Thinking null is printed as 'null'
  • Assuming compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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