Why do we use .NET properties instead of plain old

2019-02-01 22:23发布

I understand the many benefits of providing an interface to access the members of a class indirectly. My question is: isn't that already something you can accomplish in just about any OO language using (something along the lines of)

public int NormalClass::getQuality() {
    return this->quality;
}

and

protected void NormalClass::setQuality(int q) {
    this->quality = q;
}

?

What additional benefits do .NET properties offer, beyond sheer aesthetics?

I will accept "readability" if you can make a compelling argument for it; but personally, I'm inclined to think a get/set function is more readable than a property since it is unambiguously a function as opposed to a straight-up value.

EDIT: Thanks to everybody for answering! This has been really informative for me; to sum up what I've gathered/learned from all that has been said, the following are a few conclusions I've reached so far:

  • The greatest benefits of properties come not from specific features of properties themselves, but rather from framework and IDE features which handle properties in a special way; e.g., the Properties editor, XML serialization, data binding.
  • Properties can be treated as simple values in certain convenient ways that get/set functions can't: in particular, obj.Prop++ and obj.Prop = value.
  • Properties let you get away with quick and dirty code using public members without going through the headache of implementing a bunch of get/set functions later; if you should ever need to add some logic and/or make a public member private, you can simply introduce a property and not risk breaking any old code.

Now, there is one point that's been made in 2 or 3 of the answers so far that I personally find somewhat dubious: that properties imply inexpensive read/write operations and can therefore be used in essentially the same way as simple variables. My issue with this point is that there is nothing inherent in properties that actually enforces this; it is simply how they are supposed to be used. To me, this is analogous to a "shouldBePrivate" qualifier that indicates a value should be accessed directly only by its own class, but which can still be accessed externally anyway; or a police force that patrols the streets to remind us that we should behave ourselves, but does not actually interfere when we start committing crimes (if it isn't enforced, what is it really doing for us?).

I'd be more impressed by this point if properties had some sort of built-in mechanism for ensuring read/write cheapness.

15条回答
闹够了就滚
2楼-- · 2019-02-01 22:45

for me, it's easy:

myprop = myvalue;
console.writeline(myprop);

no need for

mysetfunc(myvalue);
console.writeline(mygetprop);

easier to remember 1 thing than 2

查看更多
甜甜的少女心
3楼-- · 2019-02-01 22:52

They allow the user of the type to have a simplified syntax, and allow you to create read-only and write-only fields. Thus me.SetAge(34); age = me.GetAge(); becomes me.age = 34; age = me.age;

查看更多
【Aperson】
4楼-- · 2019-02-01 22:53

One of the popular ways of looking at object-oriented programming is to model the classes in our programs on the concepts in our heads.

The concepts in our heads are based on the actual objects that we perceive around us (whether we perceive them or we communicate with others who have perceived them).

The objects around us - furniture, animals, space shuttles, etc. - have specific properties and act in specific ways.

That's where we get properties and methods.

In C#, a property may not be reducible to a single field-get or field-set (for example, it may require additional checks or there may be caching involved or any number of reasons). So we need a separate concept of properties with get-methods and set-methods to make our programs closer to the concepts we want them to model.

查看更多
戒情不戒烟
5楼-- · 2019-02-01 22:53

As usr states:

"A property has a connotation of get being without side effects and both get and set being a fast operation."

Exactly. It is implied that a getter/setter will be quick. By exposing something as property you're implying that you're quickly fetching/putting an attribute on an object. Methods are for doing some form of work assumed to involved more cycles than simply getting/setting an attribute. We usually will put a lengthy operation 'properties' into a GetFoo(...)/SetFoo(...) methods to indicate that the computation operation is heavier than a property.

查看更多
做个烂人
6楼-- · 2019-02-01 22:55

I believe XML Serialization only reads/writes public properties, so your get and set methods would be ignored.

Also, if you have a generic list of objects, you can assign that to a DataGridView.DataSource and you'll get a column for each of your properties. That may be what @LPalmer was referring to.

查看更多
虎瘦雄心在
7楼-- · 2019-02-01 22:56

I know in some instances, you can use a property as a "Column" name like in a data set. I think .NET does this through introspection. I don't beleive this is possible with get/set functions.

查看更多
登录 后发表回答