Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Do I need to check this.Disposed inside my get/set for properties, too?

Do I need to check this.Disposed inside my get/set for properties, too?

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    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 my Dispose() method 3. "test this.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 test this.disposed in the get/set clauses of my properties? Or just in my "regular methods"?

    S P S 3 Replies Last reply
    0
    • J JoeRip

      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 my Dispose() method 3. "test this.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 test this.disposed in the get/set clauses of my properties? Or just in my "regular methods"?

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • J JoeRip

        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 my Dispose() method 3. "test this.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 test this.disposed in the get/set clauses of my properties? Or just in my "regular methods"?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J JoeRip

          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 my Dispose() method 3. "test this.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 test this.disposed in the get/set clauses of my properties? Or just in my "regular methods"?

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          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 calling Dispose() 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 implement IDisposable 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 an ObjectDisposedException and then just call that as the first line in any of your methods or properties. If you are using IDisposable 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups