#251 – Class Properties Support the Principle of Encapsulation

Encapsulation is one of the core principles of object-oriented programming.  Encapsulation is the idea of hiding implementation details of a class from the users of that class and only exposing a public interface.

Class properties in C# support the idea of encapsulation.  Client code that wants to read or write a property has access only to its public interface–the data type of the property and the knowledge that there is a get or a set accessor (or both) for that property.

Client code that reads or writes a property has no information about:

  • How the class actually stores the property data
  • The implementation of the get accessor–how data is retrieved when a client reads the property value
  • The implementation of the set accessor–how data is stored when a client writes the property value