Access Modifiers in C#

This article describes about access modifiers in OOP or C# access modifiers. Summary of the article:

  • Access Modifiers
  • Why we should use Access Modifiers?
  • Types of Access Modifiers
  • Default Access Modifier

Access Modifiers
Access modifiers determine the scope of the method or variables that can be accessed from other various objects or classes. Access modifiers are keywords used to specify the accessibility of a member or a type. In a program access modifier allows us a way to handle which member or type has an access or not to certain features. For example if you want to hide or encapsulate a particular member or type outside the class scope then you achieve this through access modifier. Or if you want to expose particular member or type outside the class scope then you achieve this through access modifier.

Why we should use Access Modifiers?
Access modifiers are very essential part of Object Oriented Programming or OOP. They support the concept of encapsulation which promotes the idea of hiding functionality. Access modifiers allow us to define who does or doesn’t have access to certain features.

Types of access modifiers
Types of access modifiers in C# are:

  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected Internal

Public Access Modifier
Used in based or derived class.
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.

Example:
Example of public access modifier is given bellow:

public class Employee
{
    public string EmployeeName()
    {
        //Write code
    }
}

Private Access Modifier
Only used inside the class.
Access is limited to within the class.
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.

Example:
Example of private access modifier is given bellow:

public class Employee
{
    private string EmployeeName()
    {
        //Write code
    }
}

Protected Access Modifier
Only used inside the class. But can be used in derived class.
Access is limited to within the class 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.

Example:
Example of protected access modifier is given bellow:

public class Employee
{
    protected string EmployeeName()
    {
        //Write code
    }
}

Internal Access Modifier
Not used outside the assembly.
Access is limited exclusively to classes within the current project assembly.
The type or member can be accessed only by code in the same class or struct.

Example:
Example of internal access modifier is given bellow:

{
    internal string EmployeeName()
    {
        //Write code
    }
}

Protected Internal Access Modifier
Not used outside the assembly. But can be used in derived class of another assembly.

Example:
Example of protected internal access modifier is given bellow:

public class Employee
{
    protected internal string EmployeeName()
    {
        //Write code
    }
}

Default Access Modifier
A default access level is used if no access modifier is specified in a member declaration. The default access level may be sufficient for a certain situation but we can change it to ensure proper application behavior. The following list defines the default access modifier for certain C# types:

Note: Interface and enumeration members are always public and no access modifiers are allowed.

Comments

  1. By St. Peter

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *