Practice mode C#

What does the C# using statement guarantee for an object such as a StreamReader?

Correct answer A. Its Dispose method is called when the block is exited, even if an exception is thrown

Explanation

The using statement works with types implementing IDisposable. The compiler expands it into try/finally and calls Dispose in the finally block, so files, sockets and database connections are released promptly even on an exception. Since C# 8 you can also write a using declaration, using var r = ...;, disposed at the end of the scope. The using directive for namespaces is a different feature.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.