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. C# and destructores

C# and destructores

Scheduled Pinned Locked Moved C#
csharpc++javadatabaseperformance
5 Posts 4 Posters 29 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
    Joaquin M Lopez Munoz
    wrote on last edited by
    #1

    It is my understanding that C#, being a garbage collected language as Java is, does not have destructors. What's funny about this is the not so commonly acknowledged fact that destructors are a programmer's friend cause they assure proper releasing of resources acquired during construction, regardless of the flow of control (exceptions included). So, not having to worry about memory leaks (which is what garbage collection is for, and what forces the absence of destructors) imply having to worry too much to avoid other types of leaks. Have you ever seen a robust snippet of code in Java (C# should look alike) managing with say SQL connections dealing with proper shutdown of those connections? Man, you got to put all the closing code on a finally statement, and moreover every closing statement wrapped by try{...}catch(Exception e){} things. Lacking destructors often results in classes with open/close methods with which the programmer has to mimick what a compiler could achieve with constructor/destructors (and not forgetting to invoke a destructor method as a programmer is likely to). In C++ nothing of this type happens, provided you are reasonably disciplined (or use properly destroyed classes) and stick to the Stroustrup's motto resource acquisiton is initialization (RAII). Just wanted to know your opinions on this subject of C# lacking destructors because of the alleged merits of garbage collection. Also, as I'm no expert in this language some of you might know of additional techniques in C# to achieve the effects of RAII. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

    E L 2 Replies Last reply
    0
    • J Joaquin M Lopez Munoz

      It is my understanding that C#, being a garbage collected language as Java is, does not have destructors. What's funny about this is the not so commonly acknowledged fact that destructors are a programmer's friend cause they assure proper releasing of resources acquired during construction, regardless of the flow of control (exceptions included). So, not having to worry about memory leaks (which is what garbage collection is for, and what forces the absence of destructors) imply having to worry too much to avoid other types of leaks. Have you ever seen a robust snippet of code in Java (C# should look alike) managing with say SQL connections dealing with proper shutdown of those connections? Man, you got to put all the closing code on a finally statement, and moreover every closing statement wrapped by try{...}catch(Exception e){} things. Lacking destructors often results in classes with open/close methods with which the programmer has to mimick what a compiler could achieve with constructor/destructors (and not forgetting to invoke a destructor method as a programmer is likely to). In C++ nothing of this type happens, provided you are reasonably disciplined (or use properly destroyed classes) and stick to the Stroustrup's motto resource acquisiton is initialization (RAII). Just wanted to know your opinions on this subject of C# lacking destructors because of the alleged merits of garbage collection. Also, as I'm no expert in this language some of you might know of additional techniques in C# to achieve the effects of RAII. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      C# does provide finalizers, which are run before an object is collected. This means that cleanup is a matter of timing (ie is the cleanup done early enough), not whether it gets done at all. C# does provide the using statement, which wraps usage of a resource that implements IDisposable (the design pattern for classes that hold onto non-memory resources) in a try-finally for you. This works well for short-lived resources, but for longer-lived ones, you are going to have to track the lifetime yourself, just as you do in C++.

      U 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        C# does provide finalizers, which are run before an object is collected. This means that cleanup is a matter of timing (ie is the cleanup done early enough), not whether it gets done at all. C# does provide the using statement, which wraps usage of a resource that implements IDisposable (the design pattern for classes that hold onto non-memory resources) in a try-finally for you. This works well for short-lived resources, but for longer-lived ones, you are going to have to track the lifetime yourself, just as you do in C++.

        U Offline
        U Offline
        Uwe Keim
        wrote on last edited by
        #3

        Regarding your statement about "using" and "IDisposable": I search through all MSDN-documentation that comes with Beta 2 and found nearly nothing about that topic. Would you be so kind to provide a link or a short example? -- See me: www.magerquark.de Want a job? www.zeta-software.de/jobs

        L 1 Reply Last reply
        0
        • U Uwe Keim

          Regarding your statement about "using" and "IDisposable": I search through all MSDN-documentation that comes with Beta 2 and found nearly nothing about that topic. Would you be so kind to provide a link or a short example? -- See me: www.magerquark.de Want a job? www.zeta-software.de/jobs

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I'd try this post. http://discuss.develop.com/archives/wa.exe?A2=ind0010A&L=DOTNET&P=R28572

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            It is my understanding that C#, being a garbage collected language as Java is, does not have destructors. What's funny about this is the not so commonly acknowledged fact that destructors are a programmer's friend cause they assure proper releasing of resources acquired during construction, regardless of the flow of control (exceptions included). So, not having to worry about memory leaks (which is what garbage collection is for, and what forces the absence of destructors) imply having to worry too much to avoid other types of leaks. Have you ever seen a robust snippet of code in Java (C# should look alike) managing with say SQL connections dealing with proper shutdown of those connections? Man, you got to put all the closing code on a finally statement, and moreover every closing statement wrapped by try{...}catch(Exception e){} things. Lacking destructors often results in classes with open/close methods with which the programmer has to mimick what a compiler could achieve with constructor/destructors (and not forgetting to invoke a destructor method as a programmer is likely to). In C++ nothing of this type happens, provided you are reasonably disciplined (or use properly destroyed classes) and stick to the Stroustrup's motto resource acquisiton is initialization (RAII). Just wanted to know your opinions on this subject of C# lacking destructors because of the alleged merits of garbage collection. Also, as I'm no expert in this language some of you might know of additional techniques in C# to achieve the effects of RAII. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Well, it is not really a problem, I think that in c# also exist something like System.gc() ( the garbage colector calling function), You can call it any time you want.

            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