Access Modifiers in C#

My intend of writing this article  is to make beginner familiar with basics of access Modifier which are used in c# programming. Also by considering the importance of Interview which are mostly asked on access modifier have differentiated each modifier with each to easily 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, methods, class and its members into the program or in application.

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

There 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 commonly used access specified 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 specified can be accessed

  • ·         Within the class in which they are declared.
  • ·         Within the derived classes of that class available within the same assembly.
  • ·         Outside the class within the same assembly.
  • ·         Within the derived classes of that class available outside the assembly.
  • ·         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 

Only Within the class in which they are declared.

Protected

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

  • ·         Within the class in which they are declared.
  • ·         Within the derived classes of that class available within the same assembly.
  • ·         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

  • ·         Within the class in which they are declared.
  • ·         Within the derived classes of that class available within the same assembly.
  • ·         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

  • ·         Within the class in which they are declared.
  • ·         Within the derived classes of that class available within the same assembly.
  • ·         Outside the class within the same assembly.
  • ·         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 hope this article is useful for all reader especially for beginners. If you have any suggestion please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode