Encapsulation is used to protect the data from being accessed by outside.It is a process that binds together code and data which is manipulates according to requirement. Encapsulation means that covering up of data under a single unit.
using System;
public class MyCode {
private String EmployeeName;
private int EmployeeId;
public String Name
{
get
{
return EmployeeName;
}
set
{
EmployeeName = value;
}
}
public int Id
{
get
{
return EmployeeId;
}
set
{
EmployeeId = value;
}
}
}
public class Program {
static public void Main()
{
MyCode obj = new MyCode();
obj.Name = "Roy";
obj.Id = 1;
Console.WriteLine("Name: " + obj.Name);
Console.WriteLine("Id: " + obj.Id);
}
}
OUTPUT
Name: Roy
Id: 1