Access Modifier in C#
Access Modifier is the keyword which decide who can access resource or object in a C# program. We can control the access level of class, method , variables in a program using a access modifier.
There are six Access Modifiers in C# which have different features and which give different levels of access to the object(class/method/variable) with which it is used.
Public Access Modifier
The objects(methods/properties) with public access modifiers can be accessed from anywhere in a project. There is no accessibility restriction for public access modifiers.
|
|
|
|
In the above program we declared msg variable public and accessed from another class Program. As public access modifier has no restriction in its access level it can be accessed from anywhere in program i.e from another class, another assembly/namespace.
Internal Access Modifier
The objects with internal access modifiers can be accessed from anywhere within the same assembly or namespaces.
|
|
|
|
In the above code we declared msg variable internal and used in another class Program. As the object with internal access modifier can be accessed in anywhere in same assembly we got output without any error.
Protected Access Modifier
Protected access modifier limits the accessibility of objects within the class in which it is created or in the derived class.
|
|
|
|
In the above code, we derived Program class from ProtectedMsg class and as msg variable is within its access level we got output. Object with protected access modifier can also be accessed from another assembly from a derived class.
|
|
|
|
Private Access Modifier
The objects with private access modifiers can only be accessed by code in the same class so It is not accessible outside the class they are created. It is used to ensure encapsulation in the program i.e process of hiding sensitive items from the user.
|
|
|
|
Private access modifier has the least access level among all the six access modifier.
In C# access modifiers can be combined like combining protected and internal forms protected internal access modifier and likewise combining private protected form private protected access modifier.
Protected Internal Access Modifier
The objects with protected internal access modifiers can only be accessed within the same assembly or from within derived classes from another assembly.
|
|
|
|
Private protected Access Modifier
The objects with private protected access modifiers can only be accessed from the same class or the derived class.
|
|
|
|
Private Protected access modifier allows an object to be accessed from derived class in the same assembly. In the above code, msg variable is accessed from Program class which is derived from the PrivateProtectedMsg class.
Default Access Modifier
When there is no access modifier is set , then a default access modifier will be used. Default Access Modifier is different for the
Default access modifier for namespace, enum, interface is public and their access modifier can’t be changed or set. The default access level for class, struct, method, variable is private.
Here is the summary of accessibility level of the Access Modifier in C#:
Access Modifier | Entire Program | Base Class | Current Assembly | Derived Class in another assembly | Derived Class in current assembly |
---|---|---|---|---|---|
Public | Yes | Yes | Yes | Yes | Yes |
Protected | No | Yes | No | Yes | Yes |
Internal | No | Yes | Yes | No | Yes |
Protected Internal | No | Yes | Yes | Yes | Yes |
Private | No | Yes | No | No | No |
Private Protected | No | Yes | No | No | Yes |
So , that was all about the access modifiers in C#.