In order to refresh and consolidate main paradigms of OOP let’s talk about encapsulation first:
Encapsulation, in object oriented programming methodology, prevents access to implementation details. It enables a group of properties, methods and other members to be considered a single unit or object.
The following benefits are:
- Protection of data from accidental corruption.
- Specification of the accessibility of each of the members of a class to the code outside the class.
- Flexibility and extensibility of the code and reduction in complexity.
- Lower coupling between objects and hence improvement in code maintainability.
Encapsulation is used to restrict access to the members of a class so as to prevent the user of a given class from manipulating objects in ways that are not intended by the designer.
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers:
- - Public – Access is not restricted
- - Private – Access is limited to the containing type
- - Protected – Access is limited to the containing class or types derived from the containing class
- - Internal – Access is limited to the current assembly
- - Protected internal – The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.