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
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
2. Private
3. Protected
4. Internal
5. Protected internal
1. By objects of the
class
2. By derived classes
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
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
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
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
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).
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
2. By derived classes
In other assembly
(internal)
1. Cannot be accessed by
object
2. Cannot be accessed by derived classes
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
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).
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).
- Cannot be accessed by object
- Can be accessed by a derived classes
In other assembly
(internal)
1. Cannot be accessed by
object
2. Cannot be accessed by derived classes
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
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
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) |
+------------------+---------+-----------+--------+----------+--------------------+
Note: BY default the
access is internal for classes if no access modifier is added.
No comments:
Post a Comment