本文介绍了为什么属性不能是只读的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题出现在这个答案的评论中.无法拥有只读属性被认为是使用字段而不是属性的潜在原因.

This question came up in the comments of this answer. The inability to have readonly properties was proposed as a potential reason to use fields instead of properties.

例如:

class Rectangle
{
   private readonly int _width;
   private readonly int _height;

   public Rectangle(int width, int height)
   {
      _width = width;
      _height = height;
   }

   public int Width { get { return _width; } }
   public int Height { get { return _height; } }
}

但是你为什么不能这样做呢?

But why can't you just do this?

public int Width { get; readonly set; }

编辑(澄清):您可以在第一个示例中实现此功能.但是为什么不能使用自动实现的属性简写来做同样的事情呢?它也不会那么混乱,因为您不必直接访问构造函数中的字段;所有访问都将通过该属性.

Edit (clarification): You can achieve this functionality in the first example. But why can't you use the auto-implemented property shorthand to do the same thing? It would also be less messy, since you wouldn't have to directly access the fields in your constructor; all access would be through the property.

编辑(更新):从 C# 6.0 开始,支持只读属性!object MyProp { get;} 这个属性可以内联(object MyProp { get; } = ...)或在构造函数中设置,但不能在其他地方设置(就像readonly字段).

Edit (update): As of C# 6.0, readonly properties are supported! object MyProp { get; } This property can be set inline (object MyProp { get; } = ...) or in the constructor, but nowhere else (just like readonly fields).

推荐答案

因为语言不允许.

这似乎是一个轻率的答案:毕竟,语言设计者可以声明,如果您在自动属性上使用 readonly 那么它意味着属性可设置,但只能在构造函数中".

This may seem like a frivolous answer: after all, the language designers could have declared that if you used readonly on an automatic property then it would mean "the property is settable but only in the constructor".

但功能不是免费的.(Eric Gunnerson 将其表述为每个功能都以 减去 100 分开始.") 实现只读自动属性需要额外的编译器工作来支持属性上的只读修饰符(它目前仅适用于字段)、生成适当的支持字段并转换 sets 的属性分配给支持字段.要支持用户可以通过声明只读支持字段和编写单行属性 getter 来合理轻松地完成的事情,需要做大量工作,而且在不实现其他功能方面,这项工作会产生成本.

But features don't come for free. (Eric Gunnerson expresses it as "Every feature starts with minus 100 points.") To implement read-only automatic properties would have required additional compiler effort to support the readonly modifier on a property (it currently applies only to fields), to generate the appropriate backing field and to transform sets of the property to assignments to the backing field. That's quite a bit of work to support something that the user could do reasonably easily by declaring a readonly backing field and writing a one-line property getter, and that work would have a cost in terms of not implementing other features.

所以,严肃地说,答案是语言设计者和实现者要么从未想过这个想法,要么——更有可能——他们认为拥有它会很好,但决定有更好的地方来度过他们的有限的资源.没有任何技术限制会阻止语言设计者和实现者提供您建议的功能:原因更多地与软件开发的经济性有关.

So, quite seriously, the answer is that either the language designers and implementers either never thought of the idea, or -- more likely -- they thought it would be nice to have, but decided there were better places to spend their finite resources. There's no technical constraint that prevents the language designers and implementers providing the feature you suggest: the reasons are more about the economics of software development.

这篇关于为什么属性不能是只读的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:44