C# Encapsulation - c# - c# tutorial - c# net



What is encapsulation in C# ?

  • Encapsulation is used to hide its members from outside class or interface, whereas abstraction is used to show only essential features.
  • Encapsulation is implemented by using access specifiers.
  • An access specifier defines the scope and visibility of a class member.
  • In C# programming, Encapsulation uses 5 types of modifier to encapsulate data.
 encapsulation

C# supports the following access modifiers:

  • Public
  • Private
  • Protected
  • Internal
  • Protected internal

Public

  • In c#, the class member, that is defined as public can be accessed by other class member that is initialized outside the class.
  • A public member can be accessed from anywhere even outside the namespace.

Private

  • The private access specifiers restrict the member variable or function to be called outside from the parent class
  • Private access specifier allows a class to hide its member variables and member functions from other functions and objects.

Protected

  • The protected access specifier hides its member variables and functions from other classes and objects.
  • This type of variable or function can only be accessed in child class. It becomes very important while implementing inheritance.

Internal:

  • Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly.
  • The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace.

Protected internal

  • The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, but a child class within the same application.
  • Protected internal is also used while implementing inheritance.
learn c# tutorials - encapsulation vs abstaction in c#

encapsulation vs abstaction csharp in c# Example


Related Searches to C# Encapsulation