C# – Access Modifiers

Access Modifiers – C# or access specifiers are used to define the scope of the class or its members defined in the class. That means, it restricts to access the classes and its’ members within the defined scope only; out-side the scope the classes or its’ members cannot be accessed.

‘C#’ provides the following access modifiers:

  • public
  • private
  • protected
  • internal
  • protected internal

These access specifiers are used in class level and its’ members level. If none of the access specifier is specified, by default ‘C#’ treats it as an internal access-specifier.

Lets discuss about these.

public Access Modifier

The class or its’ members created with public access modifier has a global scope; that means, it can be accessible from anywhere. We can access the class with in the assembly, with in the namespace and even from another assembly.

The class members created with this access specifier can be accessible from anywhere. Remember that class level access specifiers have more preference. For example, if you declare your class with internal access specifier, and your class member is public; the class member can be accessible with in its own assembly only. Not accessible to other assembly. Because class’s member is declared as public but class itself is declared as internal.

private Access Specifier

We cannot declare a class as private in its namespace. But we can create a class as a private within another class or within another class’s members. Private classes are accessible to within the class; where the class is defined.

private members can be accessible to only with in the class. Not to even its derived classes.

protected Access Specifier

A class is accessible to its derived classes only. But we cannot declare the class as protected in its namespace. We can create a protected class within another class or within another class’s member functions.

protected members are accessible within the class or its derived classes. The derived class can be in other assemblies also.

internal Access Specifier

If no access specifier is specified, by default ‘C#’ treats it as an internal access modifier. We use internal keyword to create an internal class. Internal classes are accessible within the namespace, within the assembly. But we can’t access internal classes outside the namespace.

Internal members are accessible to all the classes within the namespace. But not accessible from outside the namespace.

protected internal Access Specifier

Classes with protected internal access specifiers can be accessed within its derived classes or any class defined in the same assembly. We cannot create the classes with protected internal access specifier within the namespaces.

protected internal class members can be accessible within its derived classes and the derived classes must be within the same namespace.

If we try to create a class using private, protected, or protected internal in the namespace; we will get below Error:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

These are the access specifiers or modifiers supported in C#. I will try to explain more about these with Examples in my upcoming articles.

(Raju)

C# – Access Modifiers

4 thoughts on “C# – Access Modifiers

Leave a Reply

Your email address will not be published.

Scroll to top