问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/45610419/why-would-vs-2017-suggest-replacing-a-property-with-a-method