Fields vs Properties for private class variables [

2019-03-11 16:58发布

This question already has an answer here:

For private class variables, which one is preferred?

If you have a property like int limit, you want it to be:

int Limit {get; set;}

and use it inside the class, like so:

this.Limit

Is there a reason to use it or not use it? Maybe for performance reasons?

I wonder if this is a good practice.

11条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-11 17:13

The point of automatic properties is they are very quick at creating a public access to some field in your class. Now, they offer no benefit over exposing straight up fields to the outside world, other than one big one.

Your class' interface is how it communicates with the outside world. Using automatic properties over fields allows you to change the internals of your class down the road in case you need to make setting the value of that property do something or check authorization rules or something similar on the read.

The fact that you already have a property means you can change your implementation without breaking your public interface.

Therefore, if this is just a private field, an automatic property isn't really that useful, not only that, but you can't initialize public properties at declaration like you can with fields.

查看更多
Viruses.
3楼-- · 2019-03-11 17:16

There's nothing wrong with having private or protected properties; this is mostly useful when there is some rule or side effect associated with the underlying variable.

The reason why properties seem more natural for public variables is that in the public case, it is a way to hedge one's bet against future implementation changes, whereby the property will remain intact but the implementation details somehow move around (and/or some additional business rule will be needed).

On performance, this is typically insignificant, or indeed identical for straight-assignment properties.

I personally dislike (but often use) plain assignment properties because they just clutter the code. I wish C# would allow for "after the fact refactoring".

查看更多
姐就是有狂的资本
4楼-- · 2019-03-11 17:18

If your data member need only set and get logic then properties are very good and fast solution in C#

查看更多
Summer. ? 凉城
5楼-- · 2019-03-11 17:21

The only real benefit an auto-property has over a field when the accessibility is private is that you can set a breakpoint on accesses and updates of the variable. If that is important to your scenario then definitely use an auto-property. Otherwise, given there is no substantial advantage, I choose to go with the simplest construct which is a field.

查看更多
甜甜的少女心
6楼-- · 2019-03-11 17:27

For a private member, I only make it a property when getting and/or setting the value should cause something else to occur, like:

private int Limit
{
   get
   {
       EnsureValue();
       return this._limit;
   }
}

Otherwise, fields are fine. If you need to increase their accessibility, it's already a big enough change that making it a property at that point isn't a huge deal.

Edit: as Scott reminds us in the comments, side effects in properties can often cause more pain than anything else. Don't violate Single Responsibility and limit property logic to consistent, logical operations on the value only that must be done at the gate - such as lazy loading (as in the example above), transforming an internal structure into a publicly-useful format, etc.

查看更多
登录 后发表回答