Encapsulation is defined as the grouping of data into a single unit. It is a mechanism that links code and the data it manipulates. In a different way, encapsulation is a shield that prevents access to data from codes outside that shield.
using System;
public class Encap
{
private String studentName;
private int studentAge;
public string StudentName { get => studentName; set => studentName = value; }
public int StudentAge { get => studentAge; set => studentAge = value; }
}
class GFG
{
static public void Main()
{
Encap obj = new Encap();
obj.StudentName = "shahzad";
obj.StudentAge = 27;
Console.WriteLine("Name: " + obj.StudentName);
Console.WriteLine("Age: " + obj.StudentAge);
}
}
Name: shahzad
Age: 27