为什么Microsoft建议对具有可变值的只读域? [英] Why does Microsoft advise against readonly fields with mutable values?

查看:166
本文介绍了为什么Microsoft建议对具有可变值的只读域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在href=\"http://msdn.microsoft.com/en-us/library/ms229057.aspx\">设计准则类库开发,微软说

In the Design Guidelines for Developing Class Libraries, Microsoft say:

不分配可变类型的实例为只读域。

采用可变类型创建的对象可以在创建之后进行修改。例如,数组和大多数集合是可变类型,同时的Int32,乌里和String是不可变的类型。对于持有一个可变引用类型字段,只读改性剂防止字段值被覆盖,但不保护从变形例的可变类型

The objects created using a mutable type can be modified after they are created. For example, arrays and most collections are mutable types while Int32, Uri, and String are immutable types. For fields that hold a mutable reference type, the read-only modifier prevents the field value from being overwritten but does not protect the mutable type from modification.

这只是重申只读的行为没有解释为什么这是不好只读使用。言外之意似乎是,很多人不明白什么是只读现在和将来预期错误只读域是深深的不可变的。实际上它使用建议只读为代码文件说明深不变性 - 尽管事实上,编译器没有办法执行本 - 并禁止其使用​​了其正常功能:确保字段的值后,不会改变该对象已建成。

This simply restates the behaviour of readonly without explaining why it's bad to use readonly. The implication appears to be that many people do not understand what "readonly" does and will wrongly expect readonly fields to be deeply immutable. In effect it advises using "readonly" as code documentation indicating deep immutability - despite the fact that the compiler has no way to enforce this - and disallows its use for its normal function: to ensure that the value of the field doesn't change after the object has been constructed.

我感到不安这项建议用只读,表示不是由编译器了解其正常的意义以外的东西。我觉得它鼓励人们误解的只读的含义,并进一步期待它意味着什么,该代码的作者可能没有打算。我觉得它排除使用它的地方,它可能是有用的 - 例如,显示两个可变对象之间的一些关系仍然是这些对象中的一个的寿命不变。假设读者不理解的含义的概念,只读也似乎是矛盾的,从微软的其他建议,如FxCop的的的\"Do无法初始化不必要的规则,假设你的代码的读者是在语言专家,应该知道,(例如)布尔字段将自动初始化为假,并停止从你提供,显示了冗余是的,这已经有意识地设置为false;我不只是忘了初始化。

I feel uneasy with this recommendation to use "readonly" to indicate something other than its normal meaning understood by the compiler. I feel that it encourages people to misunderstand the meaning of "readonly", and furthermore to expect it to mean something that the author of the code might not intend. I feel that it precludes using it in places it could be useful - e.g. to show that some relationship between two mutable objects remains unchanged for the lifetime of one of those objects. The notion of assuming that readers do not understand the meaning of "readonly" also appears to be in contradiction to other advice from Microsoft, such as FxCop's "Do not initialize unnecessarily" rule, which assumes readers of your code to be experts in the language and should know that (for example) bool fields are automatically initialised to false, and stops you from providing the redundancy that shows "yes, this has been consciously set to false; I didn't just forget to initialize it".

所以,首先最重要的是,为什么微软建议不要使用只读用于向可变类型的引用的我也有兴趣知道:

So, first and foremost, why do Microsoft advise against use of readonly for references to mutable types? I'd also be interested to know:


  • 请您按照所有的代码这个设计指南?

  • 请您期待什么,当你在一段代码见只读你没有写?

推荐答案

我同意你的看法完全,然后我做有时会使用只读在我的可变引用类型的代码

I agree with you completely, and I do sometimes use readonly in my code for mutable reference types.

作为一个例子:我有可能一些私人保护成员 - 说,一个列表< T> - 我一类的在其所有的可变荣耀方法中使用(调用添加删除等)。我可能只是想提出一个保障措施到位,以保证,不管是什么,我总是处理同一个对象。这样可以保护我和从做一些愚蠢的其他开发商:即分配成员的新对象。

As an example: I might have some private or protected member -- say, a List<T> -- which I use within a class's methods in all its mutable glory (calling Add, Remove, etc.). I may simply want to put a safeguard in place to ensure that, no matter what, I am always dealing with the same object. This protects both me and other developers from doing something stupid: namely, assigning the member to a new object.

对我来说,这往往是使用属性较好的替代方案有私人设置方法。为什么?因为只读用于的值不能实例化后改变,即使是基类

To me, this is often a preferable alternative to using a property with a private set method. Why? Because readonly means the value cannot be changed after instantiation, even by the base class.

在换句话说,如果我有这样的:

In other words, if I had this:

protected List<T> InternalList { get; private set; }



然后我还可以设置 InternalList =新的List< T>() ; 在我的基类代码中任意点。 (这将需要一个非常愚蠢的错误在我的部分,是的,但它仍然是可能的。)

Then I could still set InternalList = new List<T>(); at any arbitrary point in code in my base class. (This would require a very foolish error on my part, yes; but it would still be possible.)

在另一方面,这样的:

protected readonly List<T> _internalList;



使得它的明白无误地 _internalList 永远只能的是指一个特定的对象(一到 _internalList 在构造函数中设置)。

Makes it unmistakably clear that _internalList can only ever refer to one particular object (the one to which _internalList is set in the constructor).

于是我就在你身边。一个人应该使用只读避免这个想法上一个可变引用类型是令人沮丧的我个人来说,因为它基本前提的只读一个误区关键字。

So I am on your side. The idea that one should refrain from using readonly on a mutable reference type is frustrating to me personally, as it basically presupposes a misunderstanding of the readonly keyword.

这篇关于为什么Microsoft建议对具有可变值的只读域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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