Skip to Content
All posts

Access Modifiers In C#

5 min read ·  — #csharp-interview#junior#oop

Access Modifiers In C#

Access modifiers in C# are used to specify the scope of accessibility of a member of a class or type of the class itself. For example, a public class is accessible to everyone without any restrictions, while an internal class may be accessible to the assembly only.

Why to use access modifiers?

Access modifiers are an integral part of object-oriented programming. Access modifiers are used to implement encapsulation of OOP. Access modifiers allow you to define who does or who doesn't have access to certain features.

In C# there are 6 different types of Access Modifiers.

Modifier Description
public There are no restrictions on accessing public members.
private Access is limited to within the class definition. This is the default access modifier type if none is formally specified
protected Access is limited to within the class definition and any class that inherits from the class
internal Access is limited exclusively to classes defined within the current project assembly
protected internal Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.
private protected Access is limited to the containing class or types derived from the containing class within the current assembly.

public modifier

The type or member can be accessed by any other code in the same assembly or another assembly that references it. In the following example, PublicClass is a public class, and PublicProperty is a public property. Both can be accessed from outside the class.


namespace AccessModifiers
{
    class Program
    {
        class Class
        {
            public int PublicProperty { get; set; }
        }

        static void Main(string[] args)
        {
            Class obj = new Class();
            //Direct access to public members
            obj.PublicProperty = 100;
            Console.WriteLine($"Number of public property is {obj.PublicProperty}");
        }
    }
}

private modifier

The type or member can be accessed only by code in the same class or struct. In the following example, PrivateProperty is a private property.

Accessibility

  • Cannot be accessed by object
  • By derived classes

namespace AccessModifiers
{
    class Program
    {
        class Class
        {
            public int PublicProperty { get; set; }
            private int PrivateProperty { get; set;}
        }

        static void Main(string[] args)
        {
            Class obj = new Class();
            //Direct access to public members
            obj.PublicProperty = 100;
            //Access to private member is not permitted
            obj.PrivateProperty = 20;
            Console.WriteLine($"Number of public property is {obj.PublicProperty}");
        }
    }
}

The above program will give compilation error, as access to private is not permissible. In the below figure you can see the private member num2 is not available.

private modifier

protected modifier

The type or member can be accessed only by code in the same class, or in a class that is derived from that class. In the following example, ProtectedProperty is a protected property.

Accessibility

  • Cannot be accessed by object
  • By derived classes

namespace AccessModifiers
{
    class Program
    {
        class BaseClass
        {
            protected int ProtectedProperty;
        }

        class DerivedClass : BaseClass
        {
            int DerivedProperty;

            static void Main(string[] args)
            {
                BaseClass ob1 = new();
                DerivedClass ob2 = new();

                ob2.ProtectedProperty = 20;

                // Access to protected member as it is inherited by the Derived class
                ob2.DerivedProperty = 90;

                Console.WriteLine($"DerivedProperty value {ob2.DerivedProperty}");
                Console.WriteLine($"ProtectedProperty value which is protected {ob2.ProtectedProperty}");
            }
        }
    }
}

protected modifier

In the above program we try to access protected member in main it is not available as shown in the picture below that ProtectedProperty is not listed in intellisense.

internal modifier

The type or member can be accessed by any code in the same assembly, but not from another assembly.

The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll). In other words, access is limited exclusively to classes defined within the current project assembly.

Accessibility

  • In same assembly (public)
  • Can be accessed by objects of the class
  • Can be accessed by derived classes

In other assembly (internal)

  • Cannot be accessed by object
  • Cannot be accessed by derived classes

protected internal modifier

The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly. It is a combination of protected and internal.

The protected internal accessibility means protected OR internal, not protected AND internal. In other words, a protected internal member is accessible from any class in the same assembly, including derived classes.

The protected internal access modifier seems to be a confusing but is a union of protected and internal in terms of providing access but not restricting. It allows:

  • Inherited types, even though they belong to a different assembly, have access to the protected internal members.
  • Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.

private protected modifier

The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.

The private protected accessibility means private OR protected, not private AND protected.

In other words, a private protected member is accessible by types derived from the containing class, but only within its containing assembly.

The private protected access modifier is valid only on types declared inside a type. It cannot be used on namespace-level types.

Default access

A default access level is used if no access modifier is specified in a member declaration. The following list defines the default access modifier for certain C# types:

enum: The default and only access modifier supported is public. No access modifiers are allowed.

class: The default access for members of a class, including nested classes, is private. It may be explicitly defined using any of the access modifiers.

interface: The default and only access modifier supported is public. No access modifiers are allowed.

struct: The default access is private with public and internal supported as well.

The default access may suffice for a given situation, but you should specify the access modifier you want to use to ensure proper application behavior.

Note: Interface and enumeration members are always public and no access modifiers are allowed.