Home
Blog
Are Properties Useful in C#
Updated
Dot Net Perls

Are Properties Useful in C#

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:

Methods, like properties, can return (get) and receive (set) values.
There is a traditional use of methods like GetName that have the same meaning as a Name property.
The excess syntax needed for properties could have been avoided, making the language easier to learn.

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.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
An RSS feed is available for this blog.
Home
Changes
© 2007-2025 Sam Allen