Search code examples
.netexceptiondisposeusing

In .NET - If 'Using' statement raises an Exception , what is the action to be performed?


  1. Call the Dispose()
  2. Don't Call the Dispose()
  3. Only Raise the Exception
  4. Raise the Exception , and also call the Dispose()

What is the correct answer ?


Solution

  • have you read here?

    The C# "using" statement results in a call to Dispose(). This is the same as Close(), which may throw exceptions when a network error occurs. Because the call to Dispose() happens implicitly at the closing brace of the "using" block, this source of exceptions is likely to go unnoticed both by people writing the code and reading the code. This represents a potential source of application errors.

    as it says "the call to Dispose() happens implicitly" so i hope that solves the problem