Bird
0
0

Why does the using statement guarantee that the file stream is closed even if an exception occurs inside its block?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - File IO
Why does the using statement guarantee that the file stream is closed even if an exception occurs inside its block?
ABecause it generates a try-finally block that calls Dispose automatically.
BBecause it prevents exceptions from being thrown inside the block.
CBecause it locks the file exclusively during the block.
DBecause it duplicates the stream to avoid closing the original.
Step-by-Step Solution
Solution:
  1. Step 1: Understand how using is implemented

    The using statement compiles into a try-finally structure.
  2. Step 2: Role of finally block

    The finally block calls Dispose on the object even if exceptions occur.
  3. Final Answer:

    Because it generates a try-finally block that calls Dispose automatically. -> Option A
  4. Quick Check:

    using = try-finally + Dispose call [OK]
Quick Trick: Using compiles to try-finally calling Dispose [OK]
Common Mistakes:
MISTAKES
  • Thinking using prevents exceptions
  • Believing using locks files exclusively
  • Assuming using duplicates streams

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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