Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


Access Specifier

Before understanding the concept of encapsulation, it is important to understand the concept of access specifier. Access specifiers defines the access type of the fields and methods of the class and there are six types of access specifier in C#.

  • public: Member fields and methods of the class are accessible from everywhere.
  • protected: Member fields and methods of the class are accessible in the same class or struct and by derived class.
  • private: Member fields and methods of the class are accessible within the same class or struct only.
  • internal: Member fields and methods of the class are accessible within the same assembly only.
  • protected internal: Member fields and methods of the class are accessible by any code in the assembly in which it's declared, or from within a derived class in another assembly.
  • private protected: Member fields and methods of the class are accessible only within its declaring assembly, by code in the same class or in a type that is derived from that class.

Example:

In the below example, a class called Circle is created which has a private field called radius. When it is accessed in another class called RunCircle, it raises an exception because it is a private field and it can not be accessed outside the class in which it is defined.

using System;

namespace MyApplication {
  class Circle {
    //class field
    private int radius = 10;
  }
  class RunCircle {
    static void Main(string[] args) {
      Circle MyCircle = new Circle();

      Console.WriteLine(MyCircle.radius);
    }
  }
}

The output of the above code will be:

main.cs(12,34): error CS0122: `MyApplication.Circle.radius' is inaccessible due to its protection level

Encapsulation

Encapsulation is a process of binding the fields and methods together in a single unit called class. It is aimed to prevent the direct access of fields and available only through methods of the class. Data encapsulation led to the important concept of data hiding in object-oriented programming also known as Data abstraction. Data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

Data Encapsulation steps:

  • Declare each field private.
  • Create public set method for each field to set the values of fields.
  • Create public get method for each field to get the values of fields.

In the below example, public set and get methods are created to access private member radius of class Circle.

using System;

namespace MyApplication {
  class Circle {
    //class field
    private int radius;
    //method to set the value of radius
    public void setRadius(int x) {
      radius = x;
    }
    //method to get the value of radius
    public int getRadius() {
      return radius;
    }    
  }
  class RunCircle {
    static void Main(string[] args) {
      Circle MyCircle = new Circle();

      //set the radius
      MyCircle.setRadius(50);
      //get the radius
      Console.WriteLine(MyCircle.getRadius());
    }
  }
}

The output of the above code will be:

50

5