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

北城余情 提交于 2019-12-08 17:24:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!