Encapsulation in C# is the process of hiding the implementation details of a class from other classes and providing a public interface for accessing and modifying the class’s properties and methods.
Here is an example of encapsulation in C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Encapsulation_Demo
{
// PROPERTIES IN C#
class Person
{
private string Name;
private int Age;
public void setName(string Name) // setter method
{
if(string.IsNullOrEmpty(Name) == true)
{
Console.WriteLine("Name is Must..");
}
else
{
this.Name = Name;
}
}
//public void getName() // getter method
//{
// if (string.IsNullOrEmpty(this.Name) == true)
// {
// }
// else
// {
// Console.WriteLine("Your name is: " + this.Name);
// }
//}
public void setAge(int Age) // setter method
{
if(Age > 0)
{
this.Age = Age;
}
else
{
Console.WriteLine("Age cannot be zero or negative");
}
}
public void getAge() // getter method
{
if (Age > 0)
{
Console.WriteLine("Your age is: " + this.Age);
}
else
{
}
}
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.setName("Adil");
p1.setAge(-27);
//p1.getName();
p1.getAge();
Console.ReadLine();
}
}
}