Thursday, 29 August 2013

Why does the Dispose pattern in C# not work more like RAII in C++

Why does the Dispose pattern in C# not work more like RAII in C++

So I was just reading about the RAII pattern for non garbage collected
languages, and this section caught my eye:
This limitation is typically encountered whenever developing custom
classes. Custom classes in C# and Java have to explicitly implement the
dispose method in order to be dispose-compatible for the client code. The
dispose method has to contain explicit closing of all child resources
belonging to the class. This limitation does not exist in C++ with RAII,
where the destructor of custom classes automatically destructs all child
resources recursively without requiring any explicit code.
Why is it that C++ can correctly track these resources that are allocated
in the RAII pattern, but we don't get this lovely Stack Unwinding with the
C# using construct?

No comments:

Post a Comment