Is Close() same as Using statement


Is Close() same as Dispose() or using statement. Is it necessary to call using statement even if Close is called?.

I know before disposing of an object, close should be called, so we close the resource and may it available to Dispose.

says Close is same as Dispose.


Is Close() same as Using statement?

No it is not.

Should you call Close() after a using?

No, it will break due to access to the disposed object.

Should I call Close() before exiting the using block?

It’s complicated. If the IDisposable interface is implemented correctly: no. Otherwise: possibly.

Close() has no relation to IDisposable interface, see:

using only applies to IDisposable inherited objects, see:

Close() and Dispose() may not considered to be related in any way.

When the IDisposable interface is correctly implemented, you may assume all clean-up necessary will be carried out. This implies an internal call to a Close() method would be carried out. This means that an explicit call to Close() should not be necessary.

The other way round; when a object is of type IDisposable and exposes a Close() method, a call to Close() will not be sufficient to properly dispose/clean-up the object. Dispose() must still be called, you can do this directly, or through the using statement.

You can also let the garbage-collector handle the Dispose() call for you (if its correctly implemented, see: Proper use of the IDisposable interface) But this is considered bad-practice, since you need to rely on a proper implementation and have no direct control over the GC timing.

Please note, a reason to implement a Close() function, is usually to give a developer a way to reuse the object. After calling Dispose() the object is considered to be marked for finalization and may not be used any more.

Answered By – Stefan

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.