C# Access Modifiers

https:/‮figi.www/‬tidea.com

In C#, access modifiers are used to control the visibility and accessibility of classes, fields, methods, and other members of a class. There are five access modifiers available in C#:

  1. public: Members with the public access modifier can be accessed from any code in the application, including code outside of the class that defines the member.

  2. private: Members with the private access modifier can only be accessed from within the class that defines the member. Code outside of the class cannot access or modify the member.

  3. protected: Members with the protected access modifier can be accessed from within the class that defines the member and any derived classes. Code outside of the class hierarchy cannot access or modify the member.

  4. internal: Members with the internal access modifier can be accessed from any code in the same assembly (i.e., the same .exe or .dll file). Code in a different assembly cannot access or modify the member.

  5. protected internal: Members with the protected internal access modifier can be accessed from within the class that defines the member, any derived classes, and any code in the same assembly. Code outside of the assembly cannot access or modify the member.

Here are some examples of using access modifiers in C#:

public class MyClass
{
    public int myPublicField;        // Public field
    private int myPrivateField;      // Private field
    protected int myProtectedField;  // Protected field
    internal int myInternalField;    // Internal field
    protected internal int myProtectedInternalField; // Protected internal field

    public void MyPublicMethod()     // Public method
    {
        // Do something
    }

    private void MyPrivateMethod()   // Private method
    {
        // Do something
    }

    protected void MyProtectedMethod() // Protected method
    {
        // Do something
    }

    internal void MyInternalMethod() // Internal method
    {
        // Do something
    }

    protected internal void MyProtectedInternalMethod() // Protected internal method
    {
        // Do something
    }
}

In this example, the MyClass class defines several fields and methods with different access modifiers. The myPublicField and MyPublicMethod members are public, which means that they can be accessed from any code in the application. The myPrivateField and MyPrivateMethod members are private, which means that they can only be accessed from within the MyClass class. The myProtectedField and MyProtectedMethod members are protected, which means that they can be accessed from within the MyClass class and any derived classes. The myInternalField and MyInternalMethod members are internal, which means that they can be accessed from any code in the same assembly. The myProtectedInternalField and MyProtectedInternalMethod members are protected internal, which means that they can be accessed from within the MyClass class, any derived classes, and any code in the same assembly.

Access modifiers are an important part of creating classes in C#, and they help you to control the visibility and accessibility of the members of your classes. By choosing the appropriate access modifiers for your members, you can create classes that are easy to use, understand, and maintain.