Bird
Raised Fist0

Which of the following best describes why you should use a using statement with file streams?

easy🧠 Conceptual Q2 of Q15
C Sharp (C#) - File IO
Which of the following best describes why you should use a using statement with file streams?
ATo speed up file reading by caching data.
BTo keep the file stream open indefinitely.
CTo ensure the file stream is properly closed even if an error occurs.
DTo prevent the file from being accessed by other programs.
Step-by-Step Solution
Solution:
  1. Step 1: Consider error handling with file streams

    File operations can fail, so resources must be released even on errors.
  2. Step 2: Role of using statement

    Using guarantees Dispose is called, closing the stream safely even if exceptions happen.
  3. Final Answer:

    To ensure the file stream is properly closed even if an error occurs. -> Option C
  4. Quick Check:

    using statement = safe resource cleanup [OK]
Quick Trick: Using handles exceptions and closes streams safely [OK]
Common Mistakes:
MISTAKES
  • Thinking using keeps streams open
  • Confusing using with performance optimization
  • Believing using locks files from other programs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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