C# and destructores
-
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 bytry{...}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 -
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 bytry{...}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 DesarrolloC# 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++.
-
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++.
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
-
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
I'd try this post. http://discuss.develop.com/archives/wa.exe?A2=ind0010A&L=DOTNET&P=R28572
-
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 bytry{...}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