Access Modifiers in C#

Access modifiers are keywords used to specify the declared accessibility of a member or a type.

Why to use access modifiers?

Access modifiers are an integral part of object-oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn’t have access to certain features.

In C# there are 5 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.

  • public 
    -No restrictions to access
    -The type or member can be accessed by any other code in the same assembly or another assembly that references it.
  • protected 
    -Access is limited to within the class definition and any class that inherits from the class.
    -The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
  • internal 
    -Access is limited exclusively to classes defined within the current project assembly.
    -The type or member can be accessed only by code in the same class or struct.
  • private 
    -Access is limited to within the class definition; This is the default access modifier type if none is formally specified.
    -The type or member can be accessed by any code in the same assembly, but not from another assembly.

What is an access modifier?

Access modifiers are keywords used to specify accessibility of a member or a type.
An access modifier allows us a way handle which member or type has an access or not has an access to certain features in a program.
For example if you want to hide or encapsulate particular member or type outside the class scope then you achieve this through access modifier only or if you want to expose particular member or type outside the class scope then you achieve this through access modifier only.

Types of access modifiers

Private
Protected
Public
Internal
Protected Internal

Private Access Modifier

The private keyword is a member access modifier.
Private members are accessible only within the body or scope of the class or the struct in which they are declared.
We can declare private access modifier to a class, method, property, structure, interfaces.
However if we do not specific any access modifier to a class, method, property, structure it will be assumed as private member only.
Example :
1 public class Employee
2 {
3     private void DisplayEmployeeDetails(){
4 Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
5     }
6 }
In above the code we have class “Employee” with “private” method “DisplayEmployeeDetails” which displays employee details.

Public Access Modifier:

The public keyword is a member access modifier.
Public members are accessible anywhere in program or application.
There is no restriction to access public access modifiers.
We can declare public access modifier to a class, method, property, structure, interfaces.
Example :
1 public class Employee
2 {
3     Public void DisplayEmployeeDetails(){
4 Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
5     }
6 }
In above the code we have class “Employee” with “public” method “DisplayEmployeeDetails” which displays employee details.

Protected Access Modifier:

The protected keyword is a member access modifier.
Protected members are accessible within the body or scope of the class or the struct in which they are declared and if protected member is declared in base class (Parent class) then it is also accessible in derived class (child class)  only if access takes place through derived class type.
We can declare protected access modifier to a class, method, property, structure, interfaces.
Example :
1 public class Employee
2 {
3     Protected void DisplayEmployeeDetails(){
4 Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
5     }
6 }
In above the code we have class “Employee” with “protected” method “DisplayEmployeeDetails” which displays employee details.

Internal Access Modifier:

The internal keyword is a member access modifier.
Internal members are accessible within the same namespace scope in which it is declared or it is accessible in same assembly or same project.
We can declare internal access modifier to a class, method, property, structure, interfaces.
Example :
1 public class Employee
2 {
3     internal void DisplayEmployeeDetails(){
4 Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
5     }
6 }
In above the code we have class “Employee” with “internal” method “DisplayEmployeeDetails” which displays employee details.

Protected Internal Access Modifier:

The protected internal keyword is a member access modifier.
Protected Internal members are accessible within the same namespace scope in which it is declared or it is accessible in same assembly or same project and it is also accessible in another assembly or project if protected internal member is declared in base class (Parent class) of first assembly then it is also accessible in derived class (child class) of other assembly if access takes place through derived class type.
We can declare protected internal access modifier to a class, method, property, structure, interfaces.
1 public class Employee
2 {
3    protected internal void DisplayEmployeeDetails(){
4     Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
5    }
6 }
In above the code we have class “Employee” with “internal” method “DisplayEmployeeDetails” which displays employee details.
So this is all about the access modifiers in csharp if you have any doubts or query please let me know through your comments.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s