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. How to dispose a class

How to dispose a class

Scheduled Pinned Locked Moved C#
questiontutorial
13 Posts 7 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.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I have a class called WriteToDoc. And i made an object of it like this: WriteToDoc writeToDoc = new WriteToDoc(); I was wondering how can i dispose the writeToDoc object? Thanks in advance!

    M P 2 Replies Last reply
    0
    • Y Yustme

      Hi, I have a class called WriteToDoc. And i made an object of it like this: WriteToDoc writeToDoc = new WriteToDoc(); I was wondering how can i dispose the writeToDoc object? Thanks in advance!

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, You would have to Implement the IDisposable interface in your class. This Interface includes the Dispose method. But why do you need this? All the best, Martin

      1 Reply Last reply
      0
      • Y Yustme

        Hi, I have a class called WriteToDoc. And i made an object of it like this: WriteToDoc writeToDoc = new WriteToDoc(); I was wondering how can i dispose the writeToDoc object? Thanks in advance!

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        If the WriteToDoc class has a dispose method associated with it, you could do the following: using (WriteToDoc writeToDoc = new WriteToDoc()) { } Dispose is automatically called when the execution block finishes. If you don't have a Dispose method, just call writeToDoc = null to set it to a null value. Note that this isn't necessary because the garbage collector will pick it up some time after the variable goes out of scope.

        the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
        Deja View - the feeling that you've seen this post before.

        Y N 2 Replies Last reply
        0
        • P Pete OHanlon

          If the WriteToDoc class has a dispose method associated with it, you could do the following: using (WriteToDoc writeToDoc = new WriteToDoc()) { } Dispose is automatically called when the execution block finishes. If you don't have a Dispose method, just call writeToDoc = null to set it to a null value. Note that this isn't necessary because the garbage collector will pick it up some time after the variable goes out of scope.

          the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
          Deja View - the feeling that you've seen this post before.

          Y Offline
          Y Offline
          Yustme
          wrote on last edited by
          #4

          Hi guys, I just implemented these two methods: public void Dispose() { Dispose(true); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SupressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); } private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. this.Dispose(); } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. //CloseHandle(handle); //handle = IntPtr.Zero; // Note disposing has been done. disposed = true; } } And in my Form i call the writeToDoc.Dispose() method like that. I copied it from msdn library. Should it work like that too? Thanks in advance!

          G 1 Reply Last reply
          0
          • Y Yustme

            Hi guys, I just implemented these two methods: public void Dispose() { Dispose(true); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SupressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); } private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. this.Dispose(); } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. //CloseHandle(handle); //handle = IntPtr.Zero; // Note disposing has been done. disposed = true; } } And in my Form i call the writeToDoc.Dispose() method like that. I copied it from msdn library. Should it work like that too? Thanks in advance!

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            You should implement the finalizer also:

            ~WriteToDoc() {
            Dispose(true);
            }

            This calls the Dispose method in case the coder fails to call it.

            --- Year happy = new Year(2007);

            Y S 2 Replies Last reply
            0
            • G Guffa

              You should implement the finalizer also:

              ~WriteToDoc() {
              Dispose(true);
              }

              This calls the Dispose method in case the coder fails to call it.

              --- Year happy = new Year(2007);

              Y Offline
              Y Offline
              Yustme
              wrote on last edited by
              #6

              Hi Guffa, Thank you very much!

              1 Reply Last reply
              0
              • P Pete OHanlon

                If the WriteToDoc class has a dispose method associated with it, you could do the following: using (WriteToDoc writeToDoc = new WriteToDoc()) { } Dispose is automatically called when the execution block finishes. If you don't have a Dispose method, just call writeToDoc = null to set it to a null value. Note that this isn't necessary because the garbage collector will pick it up some time after the variable goes out of scope.

                the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                Deja View - the feeling that you've seen this post before.

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                Having a Dispose method isn't enough by itself. Your class must implment IDisposable.


                only two letters away from being an asset

                Mircea PuiuM G S 3 Replies Last reply
                0
                • N Not Active

                  Having a Dispose method isn't enough by itself. Your class must implment IDisposable.


                  only two letters away from being an asset

                  Mircea PuiuM Offline
                  Mircea PuiuM Offline
                  Mircea Puiu
                  wrote on last edited by
                  #8

                  Mark, ... just looking at the footer of your messages ... Would there be any connection between KIA and "killed in action" ? :-)

                  SkyWalker

                  1 Reply Last reply
                  0
                  • N Not Active

                    Having a Dispose method isn't enough by itself. Your class must implment IDisposable.


                    only two letters away from being an asset

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    Mark Nischalke wrote:

                    Having a Dispose method isn't enough by itself. Your class must implment IDisposable.

                    Well, "must" is not really true. Adding the IDisposable interface to the list of implemented interfaces for the class doesn't really make any difference. It doesn't change the implementation of the class in any way. Using the Dispose method works just fine without the interface, only you can't use the class in a using block as that requires the IDisposable interface. It's just that the IDisposable interface is intended for this, so if you want to add disposability to a class in a well behaved way, you should implement the IDisposable interface. Just to be clear. :)

                    --- Year happy = new Year(2007);

                    N 1 Reply Last reply
                    0
                    • G Guffa

                      Mark Nischalke wrote:

                      Having a Dispose method isn't enough by itself. Your class must implment IDisposable.

                      Well, "must" is not really true. Adding the IDisposable interface to the list of implemented interfaces for the class doesn't really make any difference. It doesn't change the implementation of the class in any way. Using the Dispose method works just fine without the interface, only you can't use the class in a using block as that requires the IDisposable interface. It's just that the IDisposable interface is intended for this, so if you want to add disposability to a class in a well behaved way, you should implement the IDisposable interface. Just to be clear. :)

                      --- Year happy = new Year(2007);

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      Guffa wrote:

                      Well, "must" is not really true...only you can't use the class in a using block as that requires the IDisposable interface.

                      The post I was responding to makes use of using so in that context, yes it is a must. If the WriteToDoc class has a dispose method associated with it, you could do the following: using (WriteToDoc writeToDoc = new WriteToDoc()) { }


                      only two letters away from being an asset

                      P 1 Reply Last reply
                      0
                      • G Guffa

                        You should implement the finalizer also:

                        ~WriteToDoc() {
                        Dispose(true);
                        }

                        This calls the Dispose method in case the coder fails to call it.

                        --- Year happy = new Year(2007);

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

                        For a class that is this simple, you really don't need a finalizer. Writing a proper finalizer is generally a non-trivial task as there are certain rules that need to be followed. Beyond that, adding a finalizer causes the runtime to place the object in the finalization queue when it is instantiated, which causes the garbage collection system additional work. The finalizer will cause the Dispose method to be called if the coder forgets to call it, but that safety net usually comes at a higher cost than it's worth. For a different view on how to implement the Disposable pattern, check out this article: http://www.codeproject.com/useritems/idisposable.asp[^]

                        ----------------------------- In just two days, tomorrow will be yesterday.

                        1 Reply Last reply
                        0
                        • N Not Active

                          Having a Dispose method isn't enough by itself. Your class must implment IDisposable.


                          only two letters away from being an asset

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

                          The recommended way is to implement the IDisposable interface, but it certainly isn't required. Implementing the interface does allow the compiler to understand certain other conveniences, such as the using clause, but it doesn't cause any different compile-time or run-time behavior. It does, however, provide a valuable "clue" to anyone implementing the class that it has certain characteristics and expected behaviors. For more information on implementing Dispose, check out the following article. It goes in to more detail than the MSDN docs and pulls together the information from some of the people that actually wrote the GC system. http://www.codeproject.com/useritems/idisposable.asp[^]

                          ----------------------------- In just two days, tomorrow will be yesterday.

                          1 Reply Last reply
                          0
                          • N Not Active

                            Guffa wrote:

                            Well, "must" is not really true...only you can't use the class in a using block as that requires the IDisposable interface.

                            The post I was responding to makes use of using so in that context, yes it is a must. If the WriteToDoc class has a dispose method associated with it, you could do the following: using (WriteToDoc writeToDoc = new WriteToDoc()) { }


                            only two letters away from being an asset

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #13

                            Mark Nischalke wrote:

                            The post I was responding to makes use of using so in that context, yes it is a must. If the WriteToDoc class has a dispose method associated with it, you could do the following: using (WriteToDoc writeToDoc = new WriteToDoc()) { }

                            Correct, but in my original post I was responding to his question to call Dispose, which I thought (possibly erroneously) implied that he had implemented the IDispose interface.

                            the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                            Deja View - the feeling that you've seen this post before.

                            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