what is the benefit of get set for simple variable

2020-03-26 18:49发布

问题:

This question already has answers here:
Closed 7 years ago.

Possible Duplicate:
Public Fields versus Automatic Properties

I figured this would be answered someplace, but I'm not finding it in the usual places. I was wondering, what is the benefit of doing this

private int _foo;
public int foo {get {return _foo;} set{_foo = value;}}

or

public int foo {get; set;}

over just

public int foo;

I can see the benefit if more complex manipulation was required, but what is the benefit for a simple case like this?

回答1:

This question was asked many times here. There is no clear benefit evidence fo these cases (no logic inside property). Would say more, that using a field, you get some minimal (nano) speed benefit, but not relevant for 99.99% cases.

The guideline defined by MS suggests using properties, for expandibility and maintanability of your code. So following that guideline make easier work for someone that not familiar with the code to reading it.



回答2:

Actually, all the guidelines are about creating reusable libraries. There, when you create a property (using get/set), you also create the opportunity to later add code that is executed when someone gets or sets the value (like adding validation etc.) without changing the external definition of your code (and thus not needing to recompile the other libraries). But this has no value if you always recompile your whole solution and noone else is using the library.

Another benefit from using a property is that you can limit who can get or set the value. For example, everyone can get the value but only derived classes can set it (protected).

This said, it is still the recommendation to use properties always when they are public (as opposed to private fields).

I only expose fields when I need the best possible performance (like accessing the value million times in a row).

So to summarize

Benefits of properties (get/set) over fields:

  • Ability to add code later on without recompiling assemblies that reference this.
  • Ability to provide private/protected/internal set and public get (or any other combination).
  • Public fields are not CLS compliant.

Drawbacks of properties:

  • Slower to access (both read and write).
  • Can't pass as ref arguments to methods.


回答3:

See this Jon Skeet tutorial or When to use properties instead of functions for discussion of this matter. There is also a billion related questions and resources on this topic, which a Google/SE seach will expose.

I hope this helps.



回答4:

According to me, using 'get-set' is providing a property. Basically you are implementing the concept of Encapsulation while providing the get-set property to a variable.



回答5:

Imagine you have public variables in a class to store data. But, if you need to do data validation, you are unable to do so with public variables in a class. Because you have no control over them. Outsiders, other programmers, anyone who access your project could edit those public variables in a class.

MAINLY, In order to control the access to data in your class we use properties. They are still variables but within the authority of your class. These variables may be from any data type (Eg. String, Int, Bool, Objects, etc)

Properties can be impemented using three main options, based on what/how you want to achieve with a property.

Get - access to the property and retrieve its value
Let - access to the property and update its value
Set - access to the property and used with objects 

It's vital that one understands the real use of Get/Set methods. I agree with Tigaran on his comment.

If anyone is going to vode me down, I would like to know the reason!



回答6:

I think you've answered your own question.

For simple cases like this, there is no real advantage. Automatic get/set methods are created by using the shorthand properties:

public int Foo { get; set; }

For more complex examples where transformation is needed then you have the flexibility to alter things using explicit get and set methods.