Welcome to RP's venture...Here you can find some of the relative topics like ASP.Net, C#.Net,VB.Net, SQL Server, AJAX, Javascripts, Sharepoint, WPF, WCF, Silverlight, MVC, General knowledge, Jokes, Fun, Technical, Non-Technical etc.
0

Access Modifiers

Posted by Rajendra Prasad Panchati on Thursday, February 18, 2010
The access modifiers in .NET are

1. public

2. private

3. protected

4. internal

5. protected internal

public

Public means visible to everyone and everywhere.

Access cases

1. By objects of the class

2. By derived classes

private

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 derived 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.

Access cases

1. Cannot be accessed by object

2. Cannot be accessed by derived classes

protected

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.

Access cases

1. Cannot be accessed by object

2. By derived classes

internal

Internal are public to the entire assembly but private to any outside assemblies. Internal is useful when you don't want to allow other assemblies to have the functionality.

Access cases

In same assembly (public).

1. By objects of the class

2. By derived classes

In other assembly (internal)

1. Cannot be accessed by object

2. Cannot be accessed by derived classes

protected internal

Finally, we have the only compound access modifier allowed in .NET. Members marked as protected internal may be accessed only by a descendant class that's contained in the same assembly 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.

Note: that it's illegal to combine two access modifiers for a class but can only be applied to the members.

Access cases

In same assembly (protected).

1. Cannot be accessed by object

2. Can be accessed by a derived classes

In other assembly (internal)

1. Cannot be accessed by object

2. Cannot be accessed by derived classes

+------------------+---------+-----------+--------+----------+--------------------+
| | private | protected | public | internal | protected internal |
+------------------+---------+-----------+--------+----------+--------------------+
| By object | No | No | Yes | Yes | No |
+------------------+---------+-----------+--------+----------+--------------------+
| By derived class | No | Yes | Yes | Yes | Yes(Same assembly) |
+------------------+---------+-----------+--------+----------+--------------------+

|

0 Comments

About Me