Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   .NET access modifiers (http://www.go4expert.com/articles/net-access-modifiers-t2154/)

shabbir 6Dec2006 15:33

.NET access modifiers
 

Introduction



Classes and structs can be restricted so that only the program or namespace they are declared in may use them. Class members can be restricted so that only derived classes can use them, or restricted so that only classes within the current namespace or program can use them. Access modifiers are keywords added to the class, struct, or member declaration to specify these restrictions. So in a nutshell 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

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

Point to remember



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 but cannot be private or protected, while namespaces themselves are always public.

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

To summarize it
Code:

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



All times are GMT +5.5. The time now is 11:36.