Bird
0
0

What will happen if you try to access a resource after the using block ends?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Exception Handling
What will happen if you try to access a resource after the using block ends?
using (var fs = new System.IO.FileStream("file.txt", System.IO.FileMode.Open)) {
    // use fs
}
fs.ReadByte();
ARuntime ObjectDisposedException is thrown
BThe file stream reads successfully
CCompilation error due to scope
DThe file stream is reopened automatically
Step-by-Step Solution
Solution:
  1. Step 1: Understand resource disposal after using block

    The FileStream is disposed at the end of the using block, closing the file.
  2. Step 2: Accessing disposed resource causes exception

    Calling ReadByte on a disposed FileStream throws ObjectDisposedException at runtime.
  3. Final Answer:

    Runtime ObjectDisposedException is thrown -> Option A
  4. Quick Check:

    Access after using block = runtime exception B [OK]
Quick Trick: Accessing disposed resource throws ObjectDisposedException [OK]
Common Mistakes:
MISTAKES
  • Expecting successful read after disposal
  • Thinking compiler prevents access
  • Assuming resource reopens automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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