Is there a method, or some other light-weight way, to check if a reference is to a disposed object?
P.S. - This is just a curiousity (sleep well, not in production code). Yes, I know I can catch the ObjectDisposedException
upon trying to access a member of the object.
Notice the check on _disposed . If you were to call a Dispose method implementing this pattern, you could call Dispose as many times as you wanted without hitting exceptions.
If you don't use using , then it's up to you (the calling code) to dispose of your object by explicitely calling Dispose().
IDisposable is usually used when a class has some expensive or unmanaged resources allocated which need to be released after their usage. Not disposing an object can lead to memory leaks.
The Dispose method is automatically called when a using statement is used. All the objects that can implement the IDisposable interface can implement the using statement. You can use the ildasm.exe tool to check how the Dispose method is called internally when you use a using statement.
No - default implementation of IDisposable pattern does not support it
System.Windows.Forms.Control
has an IsDisposed
property which is set to true after Dispose()
is called. In your own IDisposable objects, you can easily create a similar property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With