Being primarily a C ++ developer, the lack of RAII (Initialization of Resource Acquisition) in Java and .NET has always bothered me. The fact that the cleaning burden is transferred from the author of the class to its consumer (using try finally or .NET using construct ) seems to be noticeably inferior.
I see why in Java there is no support for RAII, since all objects are on the heap, and the garbage collector does not in fact support deterministic destruction, and in .NET with the introduction of value types ( struct ) we have (apparently) an ideal candidate for RAII. The value type created on the stack has a well-defined area and you can use the C ++ destructor semantics. However, the CLR does not allow a value type to have a destructor.
My random searches found one argument that if the type of value is boxed , it falls under the jurisdiction of the garbage collector, and therefore its destruction becomes non-deterministic. I believe that this argument is not strong enough, the benefits of RAII are large enough to say that a value type with a destructor cannot be placed in a box (or used as a member of a class).
To shorten the long story, my question is : are there any other types of reason values ββthat cannot be used to implement RAII in .NET? (or do you think my argument about the obvious benefits of RAII is wrong?)
Edit: I must not have formulated the question clearly, since the first four answers missed the point. I know about Finalize and its non-deterministic characteristics, I know about the using construct, and I feel that these two options are inferior to RAII. using is another thing the class consumer should remember (how many people forgot to put StreamReader in the using block?). My question is a philosophical question about language design, why can it be improved in this way?
For example, using a generic deterministically destructible value type, I can make using and lock keywords redundant (reachable library classes):
public struct Disposer<T> where T : IDisposable { T val; public Disposer(T t) { val = t; } public T Value { get { return val; } } ~Disposer()
I cannot help but end with the quote I once saw, but I cannot find its origin.
You can accept my determinate destruction when my cold dead hand goes out of scope. --Anon
Motti Oct 06 '08 at 9:25 2008-10-06 09:25
source share