resharper - Is there any benefit to making a C# field read-only if its appropriate? -
I am working on a project that is using reciprocity. On this occasion, I get an indication that one area can be read only, does it have any performance or other advantage? I am assuming that the benefits will be quite low, or will any profit be purely meaning?
Thanks
with the example below the field was initially private, but inspired to set the resharper, it is only in the form of reading. I understand that to read it Can be set as a method, that is, it is being set in the constructor and has not been changed again, but just thinking that there is a benefit for it ...
Public Sector Market Controller: Controller {Private Readonly IA Kekets repository Market repository; Public Analysis Controller (IMcatets Repository Market Repository) {this.marketsRepository = Market Repository; }}
Edit What is the easiest way to view MSIL?
The benefit is semantically only, it will help users of your code to understand clearly that the object is created. This field can not be changed later. The compiler will prevent unwanted changes in this area. I fully agree with the following quote:
Clearly better than the underlying.
Some details:
The difference between the normal field and the field read-only is the flag in the IL initonly
about it in There is no optimization (such as constants) because in fact it allows all actions (received and set, but only in ctor). This is just an indication for the compiler: it can not be changed after creation.
.field public initonly int32 R
Comments
Post a Comment