Do I need to check this.Disposed inside my get/set for properties, too?
-
Peer pressure from this group has forced me to implement IDisposable on my class. :omg: My reading of the
Dispose()
documentation tells me that I should: 1. call Dispose() from my destructor 2. keep a private field (this.disposed
), and set it at the end of myDispose()
method 3. "testthis.disposed
in all of your class' regular methods, and throw an exception if the object is already disposed." (emphasis mine) Does this mean I also need to testthis.disposed
in theget/set
clauses of my properties? Or just in my "regular methods"? -
Peer pressure from this group has forced me to implement IDisposable on my class. :omg: My reading of the
Dispose()
documentation tells me that I should: 1. call Dispose() from my destructor 2. keep a private field (this.disposed
), and set it at the end of myDispose()
method 3. "testthis.disposed
in all of your class' regular methods, and throw an exception if the object is already disposed." (emphasis mine) Does this mean I also need to testthis.disposed
in theget/set
clauses of my properties? Or just in my "regular methods"?It's kind of an extra check. The world isn't going to end if you don't do the check, but it might add an extra bit of insurance that things aren't going wrong. You should never access a disposed object, it can cause all kinds of problems. The check is only there to ensure that you don't. Personally, I do the check in _public_ methods, but tend not to bother in properties. Although technically you probably should. I'd be interested in other people's opinions on this though.
Simon
-
Peer pressure from this group has forced me to implement IDisposable on my class. :omg: My reading of the
Dispose()
documentation tells me that I should: 1. call Dispose() from my destructor 2. keep a private field (this.disposed
), and set it at the end of myDispose()
method 3. "testthis.disposed
in all of your class' regular methods, and throw an exception if the object is already disposed." (emphasis mine) Does this mean I also need to testthis.disposed
in theget/set
clauses of my properties? Or just in my "regular methods"?And then what do you do if the instance has been Disposed? " The primary use of this interface is to release unmanaged resources. " When a class doesn't directly use unmanaged resources or a resource that implements IDisposable, then I generally don't implement IDisposable on it. However, implementing IDisposble allows for the class' use in a
using
statement which is a very handy technique, so in some cases I may implement a Dispose method that does nothing or otherwise leaves the instance in a usable state. So I don't bother with tracking whether or not Dispose has been called on the instance. If Dispose has released some resource, then generally the instance's reference will be null and I can check that and either make a new instance or throw an Exception, as appropriate. -
Peer pressure from this group has forced me to implement IDisposable on my class. :omg: My reading of the
Dispose()
documentation tells me that I should: 1. call Dispose() from my destructor 2. keep a private field (this.disposed
), and set it at the end of myDispose()
method 3. "testthis.disposed
in all of your class' regular methods, and throw an exception if the object is already disposed." (emphasis mine) Does this mean I also need to testthis.disposed
in theget/set
clauses of my properties? Or just in my "regular methods"?JoeRip wrote:
call Dispose() from my destructor
Please make sure you are getting the terminology correct. In C#, the "destructor" is the
Dispose()
method. If you mean you are callingDispose()
from a method like~Class()
, then you are calling it from a finalizer. If that's the case, you need to triple-check the decision to use a finalizer and make sure that you have everything written correctly as they add a performance penalty to your object and are not trivial to write properly.JoeRip wrote:
Does this mean I also need to test this.disposed in the get/set clauses of my properties?
A lot of this really depends on why you are implementing
IDisposable
. If you are maintaining some unmanaged resources or other objects that implementIDisposable
then you would want to do this test in your public methods and properties. You can simplify this by creating a single helper method that throws anObjectDisposedException
and then just call that as the first line in any of your methods or properties. If you are usingIDisposable
simply to get the "using semantics", then you don't need to bother. You can check this article[^] for more information on how to write finalizers and the dispose pattern as well.Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai