Microsoft Certified Solutions Developer (MCSD) Certification Practice Test

Disable ads (and more) with a membership for a one time $2.99 payment

Prepare for the Microsoft Certified Solutions Developer (MCSD) Certification Exam. Practice with flashcards and multiple choice questions. Master each topic with hints and insights. Achieve your certification goals!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


What is the correct way to dispose of a FileStream object when finished?

  1. fs.Dispose();

  2. using (fs)

  3. fs.Close();

  4. using(FileStream fs = ...)

The correct answer is: using(FileStream fs = ...)

The correct method to dispose of a FileStream object properly is to utilize the `using` statement, as demonstrated in option D. This approach is advantageous because it ensures that the FileStream is automatically disposed of at the end of the block in which it is defined, even if an exception occurs. By enclosing the FileStream instantiation within the `using` statement, the code automatically calls `Dispose` on the FileStream once the execution leaves the block, which is the recommended practice for managing resources like file handles. This not only helps in releasing the underlying resources but also simplifies the code, reducing the likelihood of resource leaks due to forgetting to call Dispose or Close explicitly. Other methods, such as calling `fs.Dispose()` or `fs.Close()`, require you to remember to invoke these methods at the appropriate time, which increases the risk of forgetting to do so, particularly in error-prone areas of the code. Therefore, using the `using` statement is a best practice that enhances code clarity and correctness regarding resource management.