Properties

Even though a property presents itself as a public instance variable to the world outside the class where it is defined, it is still implemented somewhat like a method inside the class (it has an ability to execute statements). This double life makes them highly suited for replacing accessor and mutator methods. Our path to understanding properties and why they provide more consistent, clearer, and intuitive code begins with a look at accessor and mutator methods.

Properties Versus Accessor and Mutator Methods

According to the encapsulation principle, discussed in Chapter 3, instance variables of an object must be hidden from source code written outside the object. In C#, this is typically implemented by declaring all instance variables ...

Get C# Primer Plus 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.