Bird
Raised Fist0

Identify the error in this C# code snippet for writing to a file:

medium📝 Debug Q6 of Q15
C Sharp (C#) - File IO
Identify the error in this C# code snippet for writing to a file:
StreamWriter sw = new StreamWriter("data.txt");
sw.WriteLine("Data");
AFile path is invalid
BWriteLine method does not exist
CStreamWriter cannot write strings
DStreamWriter is not closed or disposed
Step-by-Step Solution
Solution:
  1. Step 1: Check resource management

    StreamWriter must be closed or disposed to flush data and release resources.
  2. Step 2: Identify missing Close or using block

    The code does not call sw.Close() or use a using statement, causing potential issues.
  3. Final Answer:

    StreamWriter is not closed or disposed -> Option D
  4. Quick Check:

    Missing StreamWriter close = Resource leak [OK]
Quick Trick: Always close or use 'using' with StreamWriter [OK]
Common Mistakes:
MISTAKES
  • Ignoring need to close StreamWriter
  • Assuming WriteLine is invalid
  • Thinking file path is wrong without evidence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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