What Distinguishes Public, Private, Protected, And Having No Access Modifier In C#?

In C#, access modifiers are used to control the accessibility of classes, methods, properties, and other members within a program. Here is a breakdown of the different access modifiers in C#:

  1. Public: The public access modifier allows unrestricted accessibility to a class, method, or property from anywhere within the program, as well as from external assemblies. Public members can be accessed by any code that has access to the containing class or assembly.

Example:

public class MyClass
{
    public int MyProperty { get; set; }  // public property
    public void MyMethod()               // public method
    {
        // ...
    }
}
  1. Private: The private access modifier restricts accessibility to within the same class. Members marked as private can only be accessed within the class they are declared. Private members are invisible and inaccessible from other classes, including derived classes.

Example:

public class MyClass
{
    private int myField;                 // private field
    private void MyMethod()              // private method
    {
       // ...
    }
}
  1. Protected: The protected access modifier allows accessibility within the same class, as well as derived classes or subclasses. Protected members are inaccessible outside the containing class, but can be accessed by derived classes that inherit from the base class.

Example:

public class MyBaseClass
{
    protected int myField;                 // protected field
    protected void MyMethod()              // protected method
    {
       // ...
    }
}

public class MyDerivedClass : MyBaseClass
{
    public void AccessProtectedMember()
    {
        myField = 10;   // Accessing protected member from derived class
        MyMethod();     // Accessing protected method from derived class
    }
}
  1. No access modifier (internal by default): When a class or member is declared without an access modifier, it is considered to have internal accessibility by default. Internal members can be accessed within the same assembly (project), but not from outside the assembly.

Example:

internal class MyClass   // internal class
{
    internal int MyProperty { get; set; }   // internal property
}

Note: There is also another access modifier called "protected internal" that provides access both within the same assembly and to derived classes.

Example:

protected internal class MyClass   // protected internal class
{
    protected internal int MyProperty { get; set; }   // protected internal property
}

Understanding and correctly using access modifiers is important for maintaining encapsulation, controlling access to sensitive data, and ensuring proper design and security in C# programs.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!