Why is there no RAII in .NET?

后端 未结 7 931
生来不讨喜
生来不讨喜 2020-12-04 14:59

Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up i

相关标签:
7条回答
  • 2020-12-04 15:40

    The closest you get to that is the very limited stackalloc operator.

    0 讨论(0)
  • 2020-12-04 15:42

    There's some similar threads if you search for them but basicly what it boils down to is that if you want to RAII on .NET simply implement an IDisposable type and use the "using" statement to get deterministic Disposal. That way many of the same ideoms can be implemented and used in only a slightly more wordy manner.

    0 讨论(0)
  • 2020-12-04 15:44

    Excellent question and one that has bothered me greatly. It appears that the benefits of RAII are perceived very differently. In my experience with .NET, the lack of deterministic (or at least reliable) resource collection is one of the major drawbacks. In fact, .NET has forced me several times to employ whole architectures to deal with unmanaged resources that might (but might not) require explicit collecting. Which, of course, is a huge drawback because it makes the overall architecture more difficult and directs the client's attention away from the more central aspects.

    0 讨论(0)
  • 2020-12-04 15:46

    A better title would be "Why is there no RAII in C#/VB". C++/CLI (The evolution of the abortion that was Managed C++) has RAII in the exact same sense as C++. It's all just syntax sugar for the same finalisation pattern that the rest of the CLI languages use (Destructors in managed objects for C++/CLI are effectively finalisers), but it is there.

    You might like http://blogs.msdn.com/hsutter/archive/2004/07/31/203137.aspx

    0 讨论(0)
  • 2020-12-04 15:51

    Brian Harry has a nice post about the rationales here.

    Here is an excerpt:

    What about deterministic finalization and value types (structs)?

    -------------- I've seen a lot of questions about structs having destructors, etc. This is worth comment. There are a variety of issues for why some languages don't have them.

    (1) composition - They don't give you deterministic lifetime in the general case for the same kinds of composition reasons described above. Any non-deterministic class containing one would not call the destructor until it was finalized by the GC anyway.

    (2) copy constructors - The one place where it would really be nice is in stack allocated locals. They would be scoped to the method and all would be great. Unfortunately, in order to get this to really work, you also have to add copy constructors and call them every time an instance is copied. This is one of the ugliest and most complex things about C++. You end up getting code executing all over the place where you don't expect it. It causes bunches of language problems. Some language designers have chosen to stay away from this.

    Let's say we created structs with destructors but added a bunch of restrictions to make their behavior sensible in the face of the issues above. The restrictions would be something like:

    (1) You can only declare them as local variables.

    (2) You can only pass them by-ref

    (3) You can't assign them, you can only access fields and call methods on them.

    (4) You can't box them.

    (5) Problems using them through Reflection (late binding) because that usually involves boxing.

    maybe more, but that's a good start.

    What use would these things be? Would you actually create a file or a database connection class that can ONLY be used as a local variable? I don't believe anybody really would. What you would do instead is create a general purpose connection and then create an auto destructed wrapper for use as a scoped local variable. The caller would then pick what they wanted to use. Note the caller made a decision and it is not entirely encapsulated in the object itself. Given that you could use something like the suggestions coming up in a couple of sections.

    The replacement for RAII in .NET is the using-pattern, which works almost as well once you get used to it.

    0 讨论(0)
  • 2020-12-04 15:52

    You can do a form of RAII in .net and java using finalize() methods. A finalize() overload is called before the class is cleaned up by the GC and so can be used to clean up any resources that absolutely shouldn't be kept by the class (mutexes, sockets, file handles, etc). It still isn't deterministic though.

    With .NET you can do some of this deterministically with the IDisposable interface and the using keyword, but this does have limitations (using construct when used required for deterministic behaviour, still no deterministic memory deallocation, not automatically used in classes, etc).

    And yes, I feel there is a place for RAII ideas to be introduced into .NET and other managed languages, although the exact mechanism could be debated endlessly. The only other alternative I could see would be to introduce a GC that could handle arbitrary resource cleanup (not just memory) but then you have issues when said resources absolutely have to be released deterministically.

    0 讨论(0)
提交回复
热议问题