Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would VS 2017 suggest replacing a property with a method?

I noticed that every property in Visual Studio 2017 has a quick action that suggests replacing it with a method.

enter image description here

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?

like image 658
mshwf Avatar asked Aug 10 '17 09:08

mshwf


People also ask

What is the difference between a property and a method?

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.

How property is different from method in C#?

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.

What is the purpose of using properties in C#?

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.

What is the difference between field and property in Javascript?

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.


1 Answers

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.

like image 148
Sergey Kalinichenko Avatar answered Oct 14 '22 08:10

Sergey Kalinichenko