Class Access Modifiers
A class can be seen and accessed by other classes in the system. The term visible is sometimes used for the term accessible. They can be used interchangeably.
There are two levels of class accessibility: public and internal.
-
A class marked
public
can be accessed by code from any assembly in the system. To make a class visible to other assemblies, use the public access modifier, as shown here:public class MyBaseClass { .... }
-
A class marked
internal
can only be seen by classes within its own assembly.This is the default accessibility level, so unless you explicitly specify the modifier public in the class declaration, code outside the assembly cannot access the class.internal class MyBaseClass { .... }
Member Access Modifiers
Member accessibility describes the visibility of the members of a class object. Each member declared in a class is visible to various parts of the system,
depending on the access modifier assigned to it in its class declaration. You’ve seen that private members are visible only to other members of the same class,
while public members can be visible to classes outside the assembly as well.
Here's some rule we need to know about member access modifiers:
Here's some rule we need to know about member access modifiers:
- All members explicitly declared in a class’s declaration are visible to each other, regardless of their accessibility specification.
- Inherited members are not explicitly declared in a class’s declaration, so, as you’ll see, inherited members might or might not be visible to members of a derived class.
-
There are five member access levels:
public
,private
,protected
,internal
,protected internal
. - Default implicit access level is
private
. - A member cannot be more accessible than its class. That is, if a class has an accessibility level limiting it to the assembly, individual members of the class cannot be seen outside the assembly, regardless of their access modifiers, even public.
Public Member Accessibility
The public access level is the least restrictive. All classes both inside and outside the assembly have free access to the member.Private Member Accessibility
The private access level is the most restrictive.- A private class member can be accessed only by members of its own class. It cannot be accessed by other classes, including classes that are derived from it.
- A private member can, however, be accessed by members of classes nested in its class.
0 comments:
Post a Comment