In object-oriented programming, an object needs to respond to requests for data. For example, a Color
object might need to return an RGB value when needed, and a name string
when that is required.
In C# we are supposed to use properties for this purpose. But do properties really have any meaningful advantage over a "Getter" method, and are they worthwhile? The generated code for a property is the same as for a GetRgb
or GetName
method.
We should use properties in C# because it is convention, and other types might expect properties for data-binding purposes. But methods could have been used instead of properties for these reasons:
GetName
that have the same meaning as a Name
property.There is no reason we must have properties in addition to methods. And at the end of the day, it is unclear whether the excess syntax and complexity is worth the benefits. And in my view, calling a method like GetName
is probably clearer than using a Name
property.