Home  »  Articles  »  C#.Net  »

Access Modifiers or Specifiers in C#

 Posted By : Manoranjan SahooPosted Date : 12/08/2011 06:11:33 AM Category : C#.Net Points : 5

 

Access Modifiers or Access Specifiers describes as the scope of accessibility of an Object and its members. All C# types and type members have an accessibility level . We gets the privilege to decide the use of access specifiers to implement encapsulation and abstraction in C#. 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# provides five access specifiers, they are as follows :

  • public
  • private
  • protected
  • internal
  • 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.

For Ex :



class ClassA
{
    public int variable1;
}
class MyMainClass
{
    static void Main(string[] args)
    {
        ClassA clsAObject=new ClassA();
        clsAObject.variable1=5; //since variable1 is public it can be accessed in other classes also
        Console.WriteLine(clsAObject.variable1);
    }
}



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.

protected :

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

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.

protected internal :

The protected internal access specifier in C# allows methods or member variables accessible to a class and its derived classes inside the same assembly or namespace within a file. These members cannot be accessed from class inside another assembly or a file.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.