Properties vs Methods

后端 未结 16 1895
傲寒
傲寒 2020-11-22 08:23

Quick question: When do you decide to use properties (in C#) and when do you decide to use methods?

We are busy having this debate and have found some areas where it

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 08:42

    From the Choosing Between Properties and Methods section of Design Guidelines for Developing Class Libraries:

    In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.

提交回复
热议问题