In C#, properties could be used to return or reset the value of private members of a class. But people can also define methods to fulfill these, so my questions are:
- why bother to introduce properties, why don't just use methods?
- private members are intended to be accessed by the class only. then why are properties defined to modify the private member?
Best How To :
Methods represent actions (think of them as verbs) and properties represent data (think of them as nouns). Properties should not perform computationally complex logic or produce side effects. Methods on the other hand should.
Why use Properties:
- Properties are be used in data binding. For example, in ASP.Net MVC you use properties as part of Models.
- Properties provide fine-grained access control. For example, you can have read-only properties, by just providing a getter.
- It helps with debugging. For example, you can set a breakpoint on the property and the IDE will break when the value of the property is touched.