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.
What I like to do is declare the objects without initializing them, but set their default values to
Nothing
. Then, at the end of the loop I write:Here is a complete sample:
This also works great for putting your main objects at the top of a routine, using them inside a
Try
routine, and then disposing them in aFinally
block:System.Windows.Forms.Control
has anIsDisposed
property which is set to true afterDispose()
is called. In your own IDisposable objects, you can easily create a similar property.