Search code examples
c#.netencapsulation

Why is encapsulation in C# important?


At the moment I'm learning C# and the author of the book I'm learning with emphasizes encapsulation.

But I don't exactly understand why.

So what are the benefits of encapsulation?


Solution

  • For the same reasons it is important in other languages.

    • Encapsulation hides the implementation of a program, module, or type.
    • It allows the type to perform logic when managing its state, thus disallowing possible invalid states for an instance.
    • It allows the type to change the way it works in the future at the source level, without bothering the code outside of it. This is good because it allows the developer to focus at one problem at a time, without having to understand every part of the code base.
    • It allows you to deploy updated versions of various modules, often without requiring the whole program to be rebuilt.