Social Icons

Monday, 24 June 2013

Acess Specifiers(Access Modifiers) in C#

Access Specifiers or Access Modifiers in C#

What is an access specifier?

Access Modifiers are used to define scope of types as well as their members i.e who can access them and who can not.

C# supports five access specifiers
  • Private
  • Protected
  • Internal
  • Protected Internal
  • Public
Note: Members defined in a type with any scope or specifier are always accessible with in the type(Same Class), restrictions comes into picture only when they try to access them outside of the type.

 Private Access Specifier in C#:

Members declared as private under a class or structure can not be accessed outside of type in which they are defined and  moreover their default scope is private only.Types can't declared as private(classes,structures), so private is applicable to members only(variables,methods,functions).
Note:Interface can not contain any private members and default scope for interface members is public. See in Diagram format

Example For Private Access Modifier:

  Protected Access Specifier in C#:

Members declared as protected under a class can be accessed only with in the class or a child class, non - child class can not consume them. Types can't be declared as protected also, so this is applicable to only members. See in Diagram format

Example for Protected Access Specifier:

In the ProtectedTypes class we declared two variables Protected x1 and public y1. In the childprotectedtype class both variables were consumed. In the childprotectedtype class we declare another protected variable z1. see in Sum() method we accessed all of the variables either protected or public. protected members can access only in children class. we can not access them using object of the class. In TestChildProtectedTypes class i can access only Y1 we can't access any protected variables, You see public variable can be accessed by anywhere.   

Internal Access Specifier in C#:

Members and types that are declared as Internal can be accessed only within the project both from child or non - child classes. The default scope for any type in c# is Internal only. These members can be accessed by using inheritance and by creating object of the class or type. This can be accessed only with in the project outside of the project it can't be accessed. See in Diagram format

Protected Internal Access Specifier in C#:

Members declared as protected internal will have dual scope. i.e with in the project they behave as internal providing access to any where in the project, outside the project they will change to protected and still provide access to their child classes. Types(Class and Structures) can not be declared as protected internal also.  See in Diagram format

   Example for Protected Internal access specifier

In the above example I created a class Protected_Internal where i declared all types of members and i accessed all of these members using inheritance and in non- child class.

Public Access Specifier in C#:

A type or a member of a type if declared as public is global in scope which can be accessed from any where. See in Diagram format

 an example public access specifier

See in the above example we can access all the public members of publictype class in child class and nonchildclass.


I hope you got some information regarding access specifiers in C# through this example. You can find diagramatic format of Access Specifiers explaination here

Related Tutorials

No comments:

Post a Comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.