10.2. Property Methods

So far you have designed and implemented the property members of the class, and you've also added two constructors to give the user some flexibility when he instantiates a clsDates object. Finally, you've written two General methods, getLeapYear() and getEaster(), that provide some functionality for the class. In the spirit of encapsulation, you defined all of the property methods using the private access specifier. Using the private access specifier makes it much more difficult to inadvertently change the value of a class property. This protection for the class properties exists because the private access specifier limits the scope of the properties to the class in which they are defined. The properties are invisible to the outside world!

Wait a minute. If the properties are invisible to the outside world because of their scope, then there is no way to change them! If the class properties can't be changed, there is no way to change the state of the object. If you can't change the state of an object, you may as well have defined a boat anchor cast in concrete. It is the ability to use and change the state of an object that makes that object useful in a program. After all, how useful can an object be if its properties are always null or 0?

10.2.1. Getters and Setters

Obviously, there's a method to this seeming madness. C# provides you with a program structure known as a property method, which is used to access private methods defined within a class. A class ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.