Member-only story

Access Modifiers in C#

Shahzad Aslam
2 min readApr 27, 2024

--

Access modifiers are keywords used to specify the declared accessibility of a member or a type.

Access Modifier in C#

The below are mentioned the access modifiers with it’s visibility and accessibility:

Public (public):

The most permissive access modifier, public allows a type or member to be accessed from any assembly or code that references it. Public members are part of the type’s public contract.

Example

public class Fruit
{
public int FruitColor;
public void getFruitColor()
{
/* ... */
}
}

Private (private):

The least permissive access modifier, private restricts access to the containing type only. Private members are not visible to other types or outside the current class.

Example

public class Fruit
{
private int FruitColor;
private void getFruitColor()
{
/* ... */
}
}

Protected (protected):

Members with the protected access modifier can be accessed within the containing class and by derived classes. They are not accessible outside of the class hierarchy.

Example

public class Fruit
{…

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet