article

Access Modifiers in .Net - An Explanation

Email
Submitted on: 1/1/2015 1:30:00 AM
By: Pankaj Nagar (from psc cd)  
Level: Beginner
User Rating: By 11 Users
Compatibility: C#, VB.NET, ASP.NET, C++.NET
Views: 1547
 
     Access modifiers are keywords which control the visibility of class members and other code constructs. But if you are confused what and when to use one Go ahead and read on... and if you find it useful plz vote.

 
				




New Page 1



.NET access modifiers

Access modifiers are keywords which control the visibility of class members and other code constructs.

The access modifiers in .NET are

1. public

2. private

3. protected

4. internal

5. protected internal

These keywords control the visibility of class members (and other things), defining the circumstances under which a member may be accessed-hence their collective name as access modifiers. With the exception of the last, protected internal, it's illegal to combine two access modifiers. Let's look at what each of these mean in turn when used in a .NET class.

Public means just that: public and visible to everyone and everything. A public member can be accessed using an instance of a class, by a class's internal code, and by any descendants of a class.

Lets say a class exists as

Class BaseFoo

Public A

Cases in which it can be accessed are as follows

1. By an instance like

Foo = new BaseFoo

Foo.A

2. By a descendent class like

Class ChildFoo

Inherits BaseFoo

MyBase.A

Private means hidden and usable only by the class itself. No code using a class instance can access a private member and neither can a descendant class. Information or functionality that will not be needed or has no meaning outside of the context of a specific class should be made private.

1. Cannot be accessed by an instance

2. Cannot be accessed by a descendent class

Protected members are similar to private ones in that they are accessible only by the containing class. However, protected members also may be used by a descendant class. So members that are likely to be needed by a descendant class should be marked protected.

1. Cannot be accessed by an Instance of the class

2. By a descendent class like

Class ChildFoo

Inherits BaseFoo

MyBase.A

Internal are public to the entire application but private to any outside applications (assembly). Internal is useful when you want to allow a class to be used by other applications but reserve special functionality for the application that contains the class.

Say a class exists in Application 1 as

Class BaseFoo

Public A

Cases in which it can be accessed from Application 1 are as follows

1. By an instance like

Foo = new BaseFoo

Foo.A

2. By a descendent class like

Class ChildFoo

Inherits BaseFoo

MyBase.A

But from an Application 2 if you have reference to BaseFoo of Application1 even then A cannot be accessed.

1. Cannot be accessed by an instance

2. Cannot be accessed by a descendent class

Finally, we have the only compound access modifier allowed in .NET,

protected internal: Members marked as protected internal may be accessed only by a descendant class that's contained in the same application as its base class. You use protected internal in situations where you want to deny access to parts of a class' functionality to any descendant classes found in other applications. Not limited to controlling class access

Say a class exists in Application 1 as

Class BaseFoo

Public A

Cases in which it can be accessed from Application 1 are as follows

1. Cannot be accessed by an instance

2. Can be accessed by a descendent class like

Class ChildFoo

Inherits BaseFoo

MyBase.A

But from an Application 2 if you have reference to BaseFoo of Application1 even then A cannot be accessed.

1. Cannot be accessed by an instance

2. Cannot be accessed by a descendent class

Access modifiers aren't limited to use on class members but can be applied to a few other code constructs. The rules defining when modifiers may be legally assigned to a construct are dependant on the construct's container:

Interface and enumeration members are always public and no access modifiers are needed (or allowed).

Classes in namespaces are internal by default and may be either internal or public, while namespaces themselves are always public.

Members of a struct are private by default and may be given public, internal, or private access modifiers.



Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.