Access Modifiers in C#

Background

My intend of writting this blog is to make begainer fammilier with bascis of access Modifier which are used in c# programming. also by considering the importance of Intrview which are mostly asked on access modifier,i have diffrenciated each modifier with each to eaisly understand. 

so let us start it with Basics..

What is Access Modifiers ?

"Access Modifiers are keywords in C# which are used to restrict avaibility of object,metho,class and its members into the program or in application".

We can control the scope of the member object of a class using access specifiers which are used to  provide security of applications.

their are following types of access modifiers in C# 

  1. Public
  2. Private
  3. Protected 
  4. Internal
  5. Protected internal
now let us i will introduce with each access modifier briefly

Public

Public is the most commonaly used access specifier in C# . It can be access from anywhere, that means there is no restriction on accessibility. The scope of the accessibility is inside class as well as outside. The type or member can be accessed by any other code in the same assembly or another assembly that references it.

some key points 

the members of public access specifier can be accessed

  1. within  the class in which they are declared.
  2.   within  the derived classes of that class available within the same assembly.
  3.  Outside the class within the same assembly.
  4.   within  the derived classes of that class available outside the assembly.
  5.  Outside the class outside the assembly. 


Private

The scope of the accessibility is limited only inside the classes or struct in which they are declared. The private members cannot be accessed outside the class and it is the least permissive access level.

some key points 

the members of Private access specifier can be accessed

  1. Only Within the class in which they are declared.

Protectd

The scope of accessibility is limited within the class or struct and the class derived from this class.


some key points 

the members of Protected access specifier can be accessed

  1. within the class in which they are declared.
  2. within  the derived classes of that class available within the same assembly.
  3. within  the derived classes of that class available outside the assembly.


Internal 

The internal access modifiers can access within the program that contain its declarations and also access within the same assembly level but not from another assembly.

some key points 

the members of Internal access specifier can be accessed

  1. within  the class in which they are declared.
  2. within  the derived classes of that class available within the same assembly.
  3. Outside the class within the same assembly.


Protected Internal

Protected internal is the same access levels of both protected and internal. It can access anywhere in the same assembly and in the same class also the classes inherited from the same class .

some key points 

the members of protected Internal access specifier can be accessed

  1. Within the class in which they are declared.
  2. Within the derived classes of that class available within the same assembly.
  3. Outside the class within the same assembly.
  4.  Within the derived classes of that class available outside the assembly.


Note


  • Namespace will not have access modifier.
  • Default access modifier for class ,struct, Interface, Enum, Delegate is Internal.
  • Default access modifier for class and struct members is private.
  • No access modifier can be applied to interface members and always interface members are public.
  • Enum members are always public and no access modifier can be applied

Summary


I have explained access modifier in short and how they differ each other,i hope this blog is useful for all reader specially for begainers. if you have any suggestion please contact me.



Ebook Download
View all
Learn
View all