Home > DeveloperSection > Blogs > Access Modifier

Access Modifier


C# C# 
Ratings:
0 Comment(s)
 1683  View(s)
Rate this:

Access modifier

Access modifiers are an integral part of object – orientedprogramming. They support the concept of encapsulation, which promotes the ideaof hiding functionality. Access modifiers allow you to define who does ordoesn’t have access to certain features.

In C# there are five different types of access Modifiers.

Modifier

Description

Public

There is no restriction on accessing public members.

Private

Access is limited to within the class definition. This is the default access modifier types if none is formally specified.

Protected

Access is limited to within the class definition and any class that inherits from the class.

Internal

Access is limited exclusively to classes defined with in the current project assembly.

Protected internal

Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.

 

Public: The public keyword is an accessmodifier for types and type members. Public access is the most permissiveaccess level.

There are no restrictions on accessing public members.

Accessibility:

*can be accessed by objects of the class

*Can be accessed by derived classes

Example:

Class accessmod

{

Public int num1;

}

Static void main()

{

Accessmod obj1=new accessmod();

Obj1.num1=100;

Console.WriteLine(“Number one value in main{0}”,ob1.num1)

Console.ReadLine();

}

}

}

Private:

Private access is the least permissive access level.

Private members are accessible only within the body of theclass or the struct in which they are declared.

Accessibility:

*cannot be accessed by object

*Cannot be accessed by derived classes

Example:

Class accessmod

{

public  int num1;

int num2;

}

Static noid main()

Accessmod obj=new accessmod();

Obj.num1=100;

Obj.num2=20;

Console.WriteLine(“Number one values in main {0}”,obj.num1);

Console.ReadLine();

}

}

Protected

A protected member is accessible from within the class inwhich it is declared, and from within any class derived from class thatdeclared this member.

A protected member of class member of a base class isaccessible in a derived class only if the access takes place through thederived class type.

Accessibility:

*cannot be accessed by object

*Can be accessed by derived classes

Example:

Class base

{

Protected int num1;

}

Class drived:base

{

Public int num2;

Static void main()

{

Base obj1=new  base();

Derived obj2=new derived();

Obj1.num1=20;

Obj2.num2=90;

Console.WriteLine(“Number two  values {0} ”,obj2.num2);

Console.WriteLine(“Number one  values which is protected {0} ”,obj2.num1);

Console.ReadLine();

}}

Internal: 

  • The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll).  
  • In other words, access is limited exclusively to classes defined within the current project assembly.

Accessibility:

In same assembly (public) 

  • Can be accessed by objects of the class
  • Can be accessed by derived classes

In other assembly (internal) 

  • Cannot be accessed by object
  • Cannot be accessed by derived classes

 

Protected internal 

The protected internalaccessibility means protected OR internal, not protected AND internal. In otherwords, a protected internal member is accessible from any class in the sameassembly, including derived classes. The protected internal access modifierseems to be a confusing but is a union of protected and internal in terms ofproviding access but not restricting. It allows:  

  • Inherited types, even though they belong to a different assembly, have access to the protected internal members.
  • Types that reside in the same assembly, even if they are not derived from the type, also have access to the protected internal members.

 

 

 

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick