Encapsulation in C# Programming

Encapsulation is the process of collecting functions and data in one unit called class. Encapsulation is also known as process of hiding data in object oriented programming languages. Encapsulation allows specify access level of a class or its members. This protects data from being corrupted and codes will be less complex, maintainable and flexible.

Encapsulation can be done by using access modifiers. They help us specify visibility of codes to outside codes. Access modifiers used on C# are:

  1. public: Can be accessed from any code in program.
  2. private: Can only be accessed by members of same class.
  3. protected: Can be accessed by members of same class and it's derived classes.
  4. internal: Can only be accessed within same assembly.
  5. protected internal: Can be accessed within same assembly and derived classes.

1. Access Modifier "public"

When a member in a class is declared with public access modifier it can be accessed anywhere else in the program. Example: 

using System;
namespace encapsulation
{
     class square
     {
         public float length;
         public float Area()
         {
             return length * length;
         }
         public void ShowArea()
         {
             Console.WriteLine("Length of a side of square: "+ length);
             Console.WriteLine("Area of square: "+ Area());
         }
     }
     class area
     {
         static void Main(string[] args)
         {
             square s = new square();
             s.length = 1.5f;
             s.ShowArea();
             Console.ReadLine();
         }
     }
}

Output:

Length of a side of square: 1.5
Area of square: 2.25

2. Access Modifier "private"

When a member in a class is declared with private access modifier it can be accessed only in that class. It cannot be accessed from any other class. "private" is the default access level of any members in class, so, if we don't use any modifier the member is private. Example: 

using System;
namespace encapsulation
{
    class square
    {
        private float length = 1.5f; //private member so, can't be use anywhere
        public double Area()
        {
            return length * length; //private member can be accessed here
        }
        public void ShowArea()
        {
            Console.WriteLine("Length of a side of square: " + length);
            Console.WriteLine("Area of square: " + Area());
        }
    }
    class area
    {
        static void Main(string[] args)
        {
            square s = new square();
            s.ShowArea();
            Console.ReadLine();
        }
    }
}

Output:

Length of a side of square: 1.5
Area of square: 2.25

3. Access Modifier "protected"


When a member in a class is declared with protected access modifier it can be accessed in that class and its derived class. Example:

using System;
namespace encapsulation
{
    class len
    {
        protected float length; //protected member
    }
    class square : len
    {
        public double Area()
        {
            this.length = 1.5f; //accessing protected member from derived class
            return length * length;
        }
        public void ShowArea()
        {
            Console.WriteLine("Area of square: " + Area());
        }
    }
    class area
    {
        static void Main(string[] args)
        {
            
            square s = new square();
            s.ShowArea();
            Console.ReadLine();
        }
    }
}

Output:

Area of square: 2.25

4. Access Modifier "internal"

When a member in a class is declared with internal access modifier it can be accessed only in that assembly. This modifier is useful when a separate assembly is created and it shouldn't access member from current assembly. Example:

using System;
namespace encapsulation
{
    class square
    {
        internal float length; //can be accessed from any class of the assembly
        internal float Area()
        {
            return length * length;
        }
        internal void ShowArea()
        {
            Console.WriteLine("Length of a side of square: " + length);
            Console.WriteLine("Area of square: " + Area());
        }
    }
    class area
    {
        static void Main(string[] args)
        {
            square s = new square();
            s.length = 1.5f;
            s.ShowArea(); //internal member is accessed here
            Console.ReadLine();
        }
    }
}

Output:

Length of a side of square: 1.5
Area of square: 2.25

In above example, internal members are easily accesses but when another library is created we cannot access it from there.

5. Access Modifier "protected internal"

When a member in a class is declared with protected internal access modifier it can be accessed only in that assembly and in derived classes. It allows access in other assembly derived class as well. Example:

using System;
namespace encapsulation
{
    class square
    {
        internal float length;
        internal float Area()
        {
            return length * length;
        }
        protected internal void ShowArea() //protected internal member
        {
            Console.WriteLine("Length of a side of square: " + length);
            Console.WriteLine("Area of square: " + Area());
        }
    }
    class area
    {
        static void Main(string[] args)
        {
            square s = new square();
            s.length = 1.5f;
            s.ShowArea(); //protected internal member is accessed here
            Console.ReadLine();
        }
    }
}

Output:

Length of a side of square: 1.5
Area of square: 2.25