Sumedh Meshram

A Personal Blog



  • Encapsulation is a procedure of covering up of the data & functions into a single unit called as class 
  • An encapsulated object is often called an abstract data type. 
  • Encapsulation can protect your data from accidental corruption. 
  • Rather than defining the data in the form of public, we can declare those fields as private.

Code Example :

public class School
	private string Schooldepartname;
	public string SchoolDepartname
			return Schooldepartname;
			Schooldepartname =value;
public class Departmentmain
	public static int Main(string[] args)
		School d= new School();
		Console.WriteLine("The Dept. Name is :{0}",d.SchoolDepartname);
		return 0;



The Dept. Name is : Communication

Benefits of Encapsulation :

  • In Encapsulation fields of a class can be read-only or can be write-only.
  • A class can have control over in its fields.
  • A class can change data type of its fields anytime but users of this class do not need to change any code.

From the above we can see the use of Encapsulation by using properties. The property has two accessor get and set. The get accessor returns the value of the some property field. The set accessor sets the value of the some property field with the contents of "value". Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation.

Add comment

  Country flag

  • Comment
  • Preview