Properties vs Methods

后端 未结 16 1668
傲寒
傲寒 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:37

    Symantically properties are attributes of your objects. Methods are behaviors of your object.

    Label is an attribute and it makes more sense to make it a property.

    In terms of Object Oriented Programming you should have a clear understanding of what is part of behavior and what is merely an attribute.

    Car { Color, Model, Brand }

    A car has Color, Model and Brand attributes therefore it does not make sense to have a method SetColor or SetModel because symantically we do not ask Car to set its own color.

    So if you map the property/method case to the real life object or look at it from symantic view point, your confusion will really go away.

提交回复
热议问题