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. using statement Vs Dispose/Finalize pattern

using statement Vs Dispose/Finalize pattern

Scheduled Pinned Locked Moved C#
visual-studiocomregextutorialannouncement
10 Posts 5 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.
  • C Offline
    C Offline
    Cracked Down
    wrote on last edited by
    #1

    What difference I see between using statement and Dispose/Finalize pattern is - “Dispose/finalize” have upper hand if any object which implements this pattern and supporting different interfaces for different operations which performed on underneath unmanaged object, can take care of releasing that object even if user forgets to call dispose on that object - However with “Using” statement, the scope of the using statement will be restricted to a single method, i.e. we will have to open the unmanaged object which will closed automatically by using’s finally block in the same method (which is, in most of the cases will not be feasible and efficient as we don’t do all the operation at a time on the unmanaged objects) For Example: 1.Dispose/Finalize: Suppose, we are using MS word COM to write doc files, we dedicate one class to do all the activities related to doc file and provide different interfaces to write to doc file like WriteTest() , InsertTable(), InsertImage(), …and so on . In this situation user will keep calling these interfaces according to their needs, and might forgets to close the word doc object. In this case, we can very well implement Dispose/Finalize pattern where finalize method will be act as back up plan to release unmanaged word doc object. This approach will be efficient as long as we are using the same unmanaged object. 2.Using statement Suppose, we are writing to a text file and we have interface to do this operation which takes text data and file name as parameter. In this case we can use “Using” statement to open the file and write data to it (again supposing that we will not any further operation on this file). What Using statement we do is it add try and finally block to this text file call. Which I guess efficient solution. Hey guys, anybody know other differences other than what I mentioned here Or you can correct me, If I am wrong in my above made conclusions!!!! :)

    C N 2 Replies Last reply
    0
    • C Cracked Down

      What difference I see between using statement and Dispose/Finalize pattern is - “Dispose/finalize” have upper hand if any object which implements this pattern and supporting different interfaces for different operations which performed on underneath unmanaged object, can take care of releasing that object even if user forgets to call dispose on that object - However with “Using” statement, the scope of the using statement will be restricted to a single method, i.e. we will have to open the unmanaged object which will closed automatically by using’s finally block in the same method (which is, in most of the cases will not be feasible and efficient as we don’t do all the operation at a time on the unmanaged objects) For Example: 1.Dispose/Finalize: Suppose, we are using MS word COM to write doc files, we dedicate one class to do all the activities related to doc file and provide different interfaces to write to doc file like WriteTest() , InsertTable(), InsertImage(), …and so on . In this situation user will keep calling these interfaces according to their needs, and might forgets to close the word doc object. In this case, we can very well implement Dispose/Finalize pattern where finalize method will be act as back up plan to release unmanaged word doc object. This approach will be efficient as long as we are using the same unmanaged object. 2.Using statement Suppose, we are writing to a text file and we have interface to do this operation which takes text data and file name as parameter. In this case we can use “Using” statement to open the file and write data to it (again supposing that we will not any further operation on this file). What Using statement we do is it add try and finally block to this text file call. Which I guess efficient solution. Hey guys, anybody know other differences other than what I mentioned here Or you can correct me, If I am wrong in my above made conclusions!!!! :)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You basically have it in one. using is not a seperate pattern. It's a piece of shorthand that allows you to define an object, and AT THAT MOMENT, know that it will be cleaned up, so you don't need to go to the bottom and have a list of objects, and check that each has Dispose called on it. It's shorthand, to make code more readable and to make it clearer that you're not leaking anything. But, it only works when an object is in the scope of a single method.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      N 1 Reply Last reply
      0
      • C Cracked Down

        What difference I see between using statement and Dispose/Finalize pattern is - “Dispose/finalize” have upper hand if any object which implements this pattern and supporting different interfaces for different operations which performed on underneath unmanaged object, can take care of releasing that object even if user forgets to call dispose on that object - However with “Using” statement, the scope of the using statement will be restricted to a single method, i.e. we will have to open the unmanaged object which will closed automatically by using’s finally block in the same method (which is, in most of the cases will not be feasible and efficient as we don’t do all the operation at a time on the unmanaged objects) For Example: 1.Dispose/Finalize: Suppose, we are using MS word COM to write doc files, we dedicate one class to do all the activities related to doc file and provide different interfaces to write to doc file like WriteTest() , InsertTable(), InsertImage(), …and so on . In this situation user will keep calling these interfaces according to their needs, and might forgets to close the word doc object. In this case, we can very well implement Dispose/Finalize pattern where finalize method will be act as back up plan to release unmanaged word doc object. This approach will be efficient as long as we are using the same unmanaged object. 2.Using statement Suppose, we are writing to a text file and we have interface to do this operation which takes text data and file name as parameter. In this case we can use “Using” statement to open the file and write data to it (again supposing that we will not any further operation on this file). What Using statement we do is it add try and finally block to this text file call. Which I guess efficient solution. Hey guys, anybody know other differences other than what I mentioned here Or you can correct me, If I am wrong in my above made conclusions!!!! :)

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        You can't compare using statement and dispose pattern. using is just a syntactic shortcut which helps to call Dispose method. There is no efficiency difference in calling Dispose directly or wrapping in a using block. Leaving disposable objects to finalization is inefficient. If the type can be disposed, you should take care about calling dispose on it once you done with the object. It looks like you are confused with managing life time of objects. :)

        Navaneeth How to use google | Ask smart questions

        C 1 Reply Last reply
        0
        • C Christian Graus

          You basically have it in one. using is not a seperate pattern. It's a piece of shorthand that allows you to define an object, and AT THAT MOMENT, know that it will be cleaned up, so you don't need to go to the bottom and have a list of objects, and check that each has Dispose called on it. It's shorthand, to make code more readable and to make it clearer that you're not leaking anything. But, it only works when an object is in the scope of a single method.

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Christian Graus wrote:

          It's a piece of shorthand that allows you to define an object, and AT THAT MOMENT, know that it will be cleaned up, so you don't need to go to the bottom and have a list of objects, and check that each has Dispose called on it.

          I liked how C++/CLI implemented this using Stack Semantics. You only need to write a destructor and compiler is smart enough to implement IDisposable on the type and ensure disposal when scope ends. IMO, it is much superior than using block.

          Navaneeth How to use google | Ask smart questions

          1 Reply Last reply
          0
          • N N a v a n e e t h

            You can't compare using statement and dispose pattern. using is just a syntactic shortcut which helps to call Dispose method. There is no efficiency difference in calling Dispose directly or wrapping in a using block. Leaving disposable objects to finalization is inefficient. If the type can be disposed, you should take care about calling dispose on it once you done with the object. It looks like you are confused with managing life time of objects. :)

            Navaneeth How to use google | Ask smart questions

            C Offline
            C Offline
            Cracked Down
            wrote on last edited by
            #5

            @Navneeth No,I am not confuse!! However I wanted to know more about it!!! :) I just wanted to see if there are some other differences other than which I have mentioned! I agree that using statement is not pattern and cant be compared with the Dispose/Finalize. However, if you think from perspective of reclaiming unmanaged objects from memory, then I am sure we can comapre them[as both do the same thing] Apart from that, what exactly pattern? its only some set of standard which anybody can define( with some advantage), so saying something like this that Using is not pattern and cant be compared with Dispose/Finalize pattern will be a harsh!! isn't it? I just posted this for discussion, which might be useful for people who do not know about these things. :) Anyways thanks for your valuable reply from N a v a n e e t h and Christian!

            N N 2 Replies Last reply
            0
            • C Cracked Down

              @Navneeth No,I am not confuse!! However I wanted to know more about it!!! :) I just wanted to see if there are some other differences other than which I have mentioned! I agree that using statement is not pattern and cant be compared with the Dispose/Finalize. However, if you think from perspective of reclaiming unmanaged objects from memory, then I am sure we can comapre them[as both do the same thing] Apart from that, what exactly pattern? its only some set of standard which anybody can define( with some advantage), so saying something like this that Using is not pattern and cant be compared with Dispose/Finalize pattern will be a harsh!! isn't it? I just posted this for discussion, which might be useful for people who do not know about these things. :) Anyways thanks for your valuable reply from N a v a n e e t h and Christian!

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

              I'll agree with Navaneeth, you seem a bit confused. "a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations." http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29[^] If your aurgument is that the using statement is a pattern, then you are also implying that statements like foreach are also patterns.


              only two letters away from being an asset

              C 1 Reply Last reply
              0
              • N Not Active

                I'll agree with Navaneeth, you seem a bit confused. "a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations." http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29[^] If your aurgument is that the using statement is a pattern, then you are also implying that statements like foreach are also patterns.


                only two letters away from being an asset

                C Offline
                C Offline
                Cracked Down
                wrote on last edited by
                #7

                not at all!!!! my argument was not that whether the using is a pattern or not I said, saying - "using is not pattern and cant be compared with other pattern" is wrong!!!!!! [only because its not a pattern]

                P 1 Reply Last reply
                0
                • C Cracked Down

                  not at all!!!! my argument was not that whether the using is a pattern or not I said, saying - "using is not pattern and cant be compared with other pattern" is wrong!!!!!! [only because its not a pattern]

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  So it's a technique, what of it?

                  1 Reply Last reply
                  0
                  • C Cracked Down

                    @Navneeth No,I am not confuse!! However I wanted to know more about it!!! :) I just wanted to see if there are some other differences other than which I have mentioned! I agree that using statement is not pattern and cant be compared with the Dispose/Finalize. However, if you think from perspective of reclaiming unmanaged objects from memory, then I am sure we can comapre them[as both do the same thing] Apart from that, what exactly pattern? its only some set of standard which anybody can define( with some advantage), so saying something like this that Using is not pattern and cant be compared with Dispose/Finalize pattern will be a harsh!! isn't it? I just posted this for discussion, which might be useful for people who do not know about these things. :) Anyways thanks for your valuable reply from N a v a n e e t h and Christian!

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    SaveTigers wrote:

                    I just wanted to see if there are some other differences other than which I have mentioned!

                    Points mentioned by you are correct. It all depends on your object's lifetime. You can use using block when your object have very small lifetime, say in a function. Consider the example 1 provided in your first post. I assume you have your word wrapper class used in a windows form.

                    class WordOperations : Form
                    {

                    WordWrapper wrapper = /\* ... \*/
                    
                    protected override void Dispose(bool disposing)
                    {
                        wrapper.Dispose();
                        /\* ...... \*/
                    }
                    

                    }

                    In the above example, we have used form's dispose method to dispose your word wrapper. This will help you to *avoid* the finalization. Now a user of your form can use it like

                    using(WordOperations op = new WordOperations())
                    {
                    op.ShowDialog();
                    }

                    This will ensure the disposal of form and the wrapper. Finalization has a cost and try to avoid it when possible. So, if you own the code base, there is no need to implement a finalizer on the wrapper class. Just make sure you call Dispose().

                    SaveTigers wrote:

                    Apart from that, what exactly pattern? its only some set of standard which anybody can define( with some advantage), so saying something like this that Using is not pattern and cant be compared with Dispose/Finalize pattern will be a harsh!! isn't it?

                    No. Implementing IDisposable in a recommended way is called as dispose pattern. using is just a syntactic sugar to ease the use of disposable objects. Read Implementing IDisposable and the Dispose Pattern Properly[^] to get a good understanding about this. :)

                    Navaneeth How to use google | Ask smart questions

                    C 1 Reply Last reply
                    0
                    • N N a v a n e e t h

                      SaveTigers wrote:

                      I just wanted to see if there are some other differences other than which I have mentioned!

                      Points mentioned by you are correct. It all depends on your object's lifetime. You can use using block when your object have very small lifetime, say in a function. Consider the example 1 provided in your first post. I assume you have your word wrapper class used in a windows form.

                      class WordOperations : Form
                      {

                      WordWrapper wrapper = /\* ... \*/
                      
                      protected override void Dispose(bool disposing)
                      {
                          wrapper.Dispose();
                          /\* ...... \*/
                      }
                      

                      }

                      In the above example, we have used form's dispose method to dispose your word wrapper. This will help you to *avoid* the finalization. Now a user of your form can use it like

                      using(WordOperations op = new WordOperations())
                      {
                      op.ShowDialog();
                      }

                      This will ensure the disposal of form and the wrapper. Finalization has a cost and try to avoid it when possible. So, if you own the code base, there is no need to implement a finalizer on the wrapper class. Just make sure you call Dispose().

                      SaveTigers wrote:

                      Apart from that, what exactly pattern? its only some set of standard which anybody can define( with some advantage), so saying something like this that Using is not pattern and cant be compared with Dispose/Finalize pattern will be a harsh!! isn't it?

                      No. Implementing IDisposable in a recommended way is called as dispose pattern. using is just a syntactic sugar to ease the use of disposable objects. Read Implementing IDisposable and the Dispose Pattern Properly[^] to get a good understanding about this. :)

                      Navaneeth How to use google | Ask smart questions

                      C Offline
                      C Offline
                      Cracked Down
                      wrote on last edited by
                      #10

                      Thanks Navaneeth for your reply !!

                      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