In C#, access modifiers are used to define the accessibility of types and their members (variables and functions) in an assembly. Therefore, the Access modifiers allow you to control which types and members can be accessed from other codes, as well as what level of access another code has to those types and members.

Access Modifier C# Placeholder Image

All Access Modifiers in C#

C# provides five access modifiers that you can use to control the accessibility of types and their members:

1. Public Access Modifier

The public access modifier allows types and members to be accessed from any code in the assembly, as well as from other assemblies that reference the assembly containing the type or member.

Example:

public class MyClass
{
    public int MyPublicProperty { get; set; }
    public void MyPublicMethod() { }
}

2. Private Access Modifier

The private access modifier restricts access to types and members to only within the same class or struct. Members that are declared as private cannot be accessed from outside the class or struct.

Example:

public class MyClass
{
    private int myPrivateField;
    private void MyPrivateMethod() { }
}

3. Protected Access Modifier

The protected access modifier allows types and members to be accessed from within the same class or struct, as well as from derived classes.

Example:

public class MyBaseClass
{
    protected int MyProtectedField;
}
public class MyDerivedClass : MyBaseClass
{
    public void MyMethod()
    {
        MyProtectedField = 42;
    }
}

4. Internal Access Modifier

The internal access modifier allows types and members to be accessed from any code within the same assembly but not from other assemblies.

Example:

internal class MyClass

{

    internal int MyInternalField;

    internal void MyInternalMethod() { }

}

5. Protected Internal Access Modifier

The protected internal access modifier allows types and members to be accessed from any code within the same assembly or from derived classes, even if those derived classes are in a different assembly.

Example:

public class MyBaseClass
{
    protected internal int MyProtectedInternalField;
}
public class MyDerivedClass : MyBaseClass
{
    public void MyMethod()
    {
        MyProtectedInternalField = 42;
    }
}

Choosing the Right Access Modifiers

When choosing an access modifier, it’s important to consider the level of access you want to provide to your types and members. Here are some tips for choosing the right access modifier:

  1. Use the public access modifier for types and members that need to be accessed from outside the assembly.
  2. Use the private access modifier for members that should not be accessed from outside the class or struct.
  3. Use the protected access modifier for members that should be accessible only within the same class or struct and their derived classes.
  4. Use the internal access modifier for members that should be accessible only within the same assembly.
  5. Use the protected internal access modifier for members that should be accessible from derived classes within the same assembly, as well as from any code within the same assembly.

Conclusion

Access modifiers are an important feature in C#, as they allow you to control the accessibility of types and their members. By choosing the right access modifier for your types and members, you can ensure that your code is secure, maintainable, and easy to use.

Categorized in: