c# - Is it possible to force an auto-property to use a readonly backing field? -
My project includes a large number of classes with those properties whose backing fields are marked as read only because they are only set to production. As a matter of style, I have an auto-property I like to use because it eliminates many boilerplate codes and encourages the use of a member of the asset instead of the banking field. However, when I use an auto-property, I lose the "readable" of my backing field. I understand that the compiler / runtime is able to take advantage of any performance improvement when the field is marked in this way, so I want to be able to mark my auto-property as read-only, something like this: < / P>
[ReadOnly] Public String LastName {get; }
instead of
Private Readonly string _LastName; Public String LastName {get {return _LlastName; }}
Is there a mechanism already available to do this? If not, is it really beneficial to actually be beneficial to read in the custom banking field?
Uncovering the area as a public is another option, I think, it seems that such an area seems wrong in front of you in a way. I.e.
public read-only string LastName;
I do not scare, but you can:
< Ex> public string LastName {get; Private set; }
readonly
is not quite as good, but not too bad.