Access Modifiers and types of access modifiers in C#

 

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 :

 
public class Employee
{
    private void DisplayEmployeeDetails(){
Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
    }
}

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 :

 
public class Employee
{
    Public void DisplayEmployeeDetails(){
Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
    }
}

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 :

 
public class Employee
{
    Protected void DisplayEmployeeDetails(){
Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
    }
}

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 :

 
public class Employee
{
    internal void DisplayEmployeeDetails(){
Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
    }
}

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.

 
public class Employee
{
   protected internal void DisplayEmployeeDetails(){
    Console.WriteLine("Employee Name --> Onlinebuff and Employee Code --> 009");
   }
}

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.

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Apr
24
2013

Difference Between Array and Array List in C#

CLICK TO READ FULL ARTICLE

Views - 41100 |Category - C#

Apr
22
2013

How to Create and Initialize Jagged Arrays using C#

CLICK TO READ FULL ARTICLE

Views - 19925 |Category - C#

Apr
20
2013

Try, Catch and Finally Blocks in C#

CLICK TO READ FULL ARTICLE

Views - 17021 |Category - C#

Apr
19
2013

Abstract Class and Abstract Method With An Example Using C#

CLICK TO READ FULL ARTICLE

Views - 49051 |Category - C#

Apr
17
2013

OOPS Principle - Abstraction in C# with an example and explanation

CLICK TO READ FULL ARTICLE

Views - 154779 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

e4ldc

 

 
Comments

Posted by Ravi pratap on 2014-07-07

Please provide the full example of Protected Internal Access Modifier