简体   繁体   English

如果适当的话,将C#字段设为只读是否有任何好处?

[英]Is there any benefit to making a C# field read-only if its appropriate?

I am working on a project using ReSharper. 我正在使用ReSharper开发一个项目。 On occasion it prompts me that a field can be made readonly. 有时它会提示我可以只读取一个字段。 Is there any performance or other benefit to this? 这有什么表现或其他好处吗? I am presuming the benefits would be quite low-level, or would any benefits be purely semantic? 我认为它的好处是相当低级的,或者任何好处都是纯粹的语义?

Thanks 谢谢

With example below the field was initially just private, but resharper prompted to set it as readonly. 在下面的示例中,该字段最初只是私有,但resharper提示将其设置为只读。 I understand the reason why it can be set as readonly, ie. 我理解为什么它可以设置为只读,即。 its being set in the constructor and not changed again, but just wondering if there are any benefits to this... 它是在构造函数中设置而不是再次更改,但只是想知道这是否有任何好处......

public class MarketsController : Controller
{
    private readonly IMarketsRepository marketsRepository;

    public AnalysisController(IMarketsRepository marketsRepository)
    {                
        this.marketsRepository = marketsRepository;
    }
}

Edit What is the easiest way to look at the MSIL? 编辑查看MSIL的最简单方法是什么?

The benefit is purely semantic. 好处纯粹是语义上的。 It will help users of your code explicitly understand that this field can't be changed after object is created. 它将帮助您的代码用户明确地了解在创建对象后无法更改此字段。 Compiler will prevent unwanted changes of this field. 编译器将阻止对此字段进行不必要的更改。 I totally agree with following quote from Python Zen : 我完全同意Python Zen的以下引用:

Explicit is better than implicit. 显式优于隐式。

Some details: 一些细节:

The only difference between normal field and read-only field is flag initonly in IL. 普通字段和只读字段之间的唯一区别是IL中的initonly标记。 There is no optimization about it (as with constants) because actually it allows all operations (get and set, but only in ctor). 它没有优化(与常量一样),因为它实际上允许所有操作(获取和设置,但仅限于ctor)。 It is just hint to compiler: don't let it be changed after construction. 它只是提示编译器:不要让它在构造后改变。

.field public initonly int32 R

It's not so much low-level performance, but more high-level maintainability. 它不是低级性能,而是更高级别的可维护性。 Making things readonly is one of the possibilities you have to limit and control the number of places a certain value can be changed. 只读内容是限制和控制某个值可以更改的位置数量的可能性之一。 This in turn means that you reduce interdependency between classes (aka "loose coupling"); 这反过来意味着你减少了类之间的相互依赖性(又名“松散耦合”); the result is an application that has fewer internal dependencies and thus a lower complexity. 结果是应用程序具有较少的内部依赖性,因此具有较低的复杂性。 In other words, readonly fields and properties make your application more maintainable. 换句话说,只读字段和属性使您的应用程序更易于维护。

It also might help spotting some bugs as well. 它也可能有助于发现一些错误。 The value is assigned in a construcotor only and this could be a problem if you forgot to change elsewhere or not. 该值仅在构造函数中分配,如果您忘记在其他地方更改,这可能是一个问题。 And if it is not supposed to be changed then you mark it as a read only. 如果它不应该被更改,那么你将它标记为只读。

我的教授在当天告诉我,宣布一些只读是一种让你的计算机误入歧途的方式。

You might be interested in this answer . 您可能对此答案感兴趣。

The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. readonly关键字用于将成员变量声明为常量,但允许在运行时计算该值。 This differs from a constant declared with the const modifier, which must have its value set at compile time. 这与使用const修饰符声明的常量不同,const修饰符必须在编译时设置其值。 Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of. 使用readonly,您可以在声明中或在该字段所属的对象的构造函数中设置字段的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM