Access Specifiers in .Net

Sunday, 15 May 2016

Access Specifiers in .Net

Access Specifiers describes as the scope of accessibility of an Object and its members.It defines the scope of a class member.
A class member refers to the variables and functions in a class. All type members have an accessibility level.
 We can control the scope of the class member using access specifiers. We are using access modifiers for providing security of our applications.
When we specify the accessibility of a type or member we have to declare it by using any of the access modifiers provided by CSharp language.

C# has 5 access specifier or access modifier keywords; those are private, public, internal, protected and protected Internal.

public :
public is the most common 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.
private:
Private access allows a class to hide its member variables and member functions from other class objects and functions. So it is not visible outside
the class. By default, the access specifier is private; if other specifier is not specified.
protected:
The protected access specifier in C# allows a class to hide its member variables and member functions from other class objects and functions, except
the child class. This access specifier is used when we need to use Inheritance in the program. In other cases (when no inheritance) protected members and types are not visible.
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. An assembly is the produced .dll or .exe from your .NET Language code (C#). Hence, if you have a C# project that has ClassA, ClassB and
ClassC then any internal type and members will become accessible across the classes with in the 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. A protected internal will be accessible within the assembly due to its internal flavor and
also via inheritance due to its protected flavor.

Example on access specifire :
Program code:-

 namespace access_specifier
 {
     class Program
     {
         static void Main(string[] args)
         {
         
           Derive obj=new Derive();
                  obj.show();
         }
     }
    class Base
    {
        public int p;
        private int x;
        internal int z;
        protected int y;
        protected internal int q;
    }
     class Derive:Base
     {
         public void show()
         {
       p=40;
             //x=10;
             z=30;
      y=20;      
             q=50;
   Console.WriteLine(+p);
       // Console.WriteLine(+x);          /*This line will return an Error since the access to this                                                                       variable is Private.  So it cannot be accessed outside the class*/
          Console.WriteLine(+z);
      Console.WriteLine(+y);      
          Console.WriteLine(+q);
          Console.ReadLine();
             
         }
     }
 }
Visibility

   keywards
Containing Classes
Derive class
Within same assembly
Outside the Application
Private
yes
No
No
No
protected
yes
yes
No
No
Internal
yes
yes
yes
No
Protected Internal
yes
yes
yes
No
Public
yes
yes
yes
yes


Nested types, which are members of other types, can have declared accessibility as indicated in the following table.

Members of
Default member accessibility
Allowed declared accessibility of the member
   enum
Public
None
   class
Private, protected, internal, private, protected internal
public
interface
Public
None
struct
Private, internal, private
public