CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

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 their members within the defined scope only; outside the scope, the classes or their’ members cannot be accessed.

‘C#’ provides the following access modifiers:

  • public
  • private
  • protected
  • internal
  • protected internal

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

Let’s discuss these.

public Access Modifier

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

The class members created with this access specifier can be accessed 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 accessed within its own assembly only. Not accessible to other assemblies. Because the class’s member is declared as public but the 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 within the class; where the class is defined.

private members can be accessed only within 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 an 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 their derived classes or any class defined in the same assembly. We cannot create the classes with protected internal access specifiers within the namespaces.

protected internal class members can be accessed 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 the 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. Required fields are marked *

Scroll to top