I noticed that every property in Visual Studio 2017 has a quick action that suggests replacing it with a method.
Is that mean that properties is not the recommended way to set and get fields value, do Microsoft intend to deprecate it in the future?!
Or are there any gains that could be achieved using methods over properties for that purpose?
In most cases, methods are actions and properties are qualities. Using a method causes something to happen to an object, while using a property returns information about the object or causes a quality about the object to change.
Ans: A property is a named attribute of an object. Properties define the characteristics of an object such as Size, Color etc. or sometimes the way in which it behaves. A method is an action that can be performed on objects.
Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set property accessor is used to assign a new value.
Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.
This is not a suggestion from Visual Studio, it is a Quick Action:
Quick Actions let you easily refactor, generate, or otherwise modify code with a single action.
Visual Studio gives you an option to convert a property to a pair of methods and a private variable as part of code refactoring, should you wish to do so for a variety of reasons.
For example, you may realize that a getter should receive an extra parameter, or the setter should have overloads on other types. In these situations a method would be required instead of a property, so Visual Studio offers you a way to do it in a few simple clicks.
For stored properties that do not have any behavior, such as the OfferPeriod
property in your class, conversion to a pair of methods does not offer any advantages over an automatic property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With