We have seen Private, Public in our previous examples or posts. These are access modifiers now let's understand different types of Access Modifiers.
Access Modifiers helps to achieve encapsulation,
Public Accessible to all classes
Private Only accessible to the same class
Protected It is accessible within the same class and its inherited classes
Internal It is accessible within its own assembly, but not from other assembly
We will be going in details to upcoming topic of Inheritance where in broad level we can see the usage of given access modifiers
Class Car
{
private string color;
public string make";
}
Class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.color = "Red"; //This will throw error of inaccesible due to its protection level
myCar.make = "Audi"; //This is allowed
}
}
Encapsulation can be achieved through modifiers, means you can hide sensitive information from clients.