为什么会出现在.net中没有RAII? [英] Why is there no RAII in .NET?

查看:160
本文介绍了为什么会出现在.net中没有RAII?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为主要是一个C ++开发人员没有 RAII(资源获得即初始化)的在Java和.NET一直困扰我。该清理的责任是从类作家搬到了其消费(由手段的事实尝试最后或.NET的的 使用结构)似乎是明显逊色。

我明白为什么在Java中存在对RAII不支持,因为所有的对象都位于堆和垃圾收集器本身不支持确定性的破坏,但在.NET中引进的值类型(结构),我们有RAII的(貌似)最合适的人选。这就是栈上创建值类型有一个明确的范围和C ++的析构函数语义都可以使用。然而,CLR不允许值类型有一个析构函数。

我的随机搜索找到了一个说法,如果一个值类型是盒装它的管辖下降垃圾收集器,因此,它的破坏变得非确定性。 我觉得这种说法是不够强大,RAII的好处是足够大了说,一个价值型与析构函数不能盒装(或用作类成员)。

为了削减长话短说,我的问题是:还有没有其他原因,值类型不能以介绍RAII到.NET中使用? (或者你认为我对RAII的明显优势的说法是有缺陷?)

编辑:我一定没有措辞的问题显然,因为前四个答案都错了。我的知道的约的Finalize 和不确定性的特点,我知道关于使用建设我觉得这两个选项都逊色于RAII。 使用一件事一类的使用者必须记住(有多少人忘了在的StreamReader 使用块?)。我的问题是关于语言的设计哲学的,为什么是它的方式,能不能改进?

有关实例与通用确定性破坏的价值型我可以使使用锁定关键字冗余(实现由库类):

 公共结构处置者< T>其中T:IDisposable的
    {
        牛逼VAL;
        公开处置者(T(T)){VAL = T; }
        公众吨价{{返回VAL; }}
        〜处置者()//目前非法
        {
            如果(VAL!=默认(T))
                val.Dispose();
        }
    }
 


我不禁有一个中肯的报价,我曾经看到结束,但目前无法找到它的起源。

  

您可以把我的确定性的破坏的时候,我冷死手超出范围。 - 匿名

解决方案

一个更好的标题是为什么会出现在C#/ VB没有RAII。 C ++ / CLI(流产的被托管C ++的演变)有RAII在完全相同的意义,因为C ++。这一切都只是语法糖的的CLI语言其余使用(析构函数为C ++ / CLI是有效finalisers管理对象)相同的定稿模式,但它的存在。

您可能会喜欢 http://blogs.msdn.com /hsutter/archive/2004/07/31/203137.aspx

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 is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior.

I see why in Java there is no support for RAII since all objects are located on the heap and the garbage collector inherently doesn't support deterministic destruction, but in .NET with the introduction of value-types (struct) we have the (seemingly) perfect candidate for RAII. A value type that's created on the stack has a well defined scope and C++ destructor semantics can be used. However the CLR does not permit a value-type to have a destructor.

My random searches found one argument that if a value-type is boxed it falls under the jurisdiction of the garbage collector and therefore its destruction becomes non-deterministic. I feel that this argument isn't strong enough, the benefits of RAII are big enough to say that a value-type with a destructor cannot be boxed (or used as a class member).

To cut a long story short my question is: are there any other reasons value types can not be used in order to introduce RAII to .NET? (or do you think my argument about RAII's obvious advantages are flawed?)

Edit: I must have not phrased the question clearly since the first four answers have missed the point. I know about Finalize and its non-deterministic characteristics, I know about the using construct and I feel these two options are inferior to RAII. using is one more thing the consumer of a class must remember (how many people forgot to put a StreamReader in a using block?). My question is a philosophical one about the language design, why is it the way it is and can it be improved?

For instance with a generic deterministically destructible value-type I can make the using and lock keywords redundant (achievable by library classes):

    public struct Disposer<T> where T : IDisposable
    {
        T val;
        public Disposer(T t) { val = t; }
        public T Value { get { return val; } }
        ~Disposer()  // Currently illegal 
        {
            if (val != default(T))
                val.Dispose();
        }
    }


I can't help but end with a apropos quotation which I once saw but can't currently find its origin.

You can take my deterministic destruction when my cold dead hand goes out of scope. --Anon

解决方案

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

这篇关于为什么会出现在.net中没有RAII?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆