Table 5-1 C# Access Modifiers-
Access Modifier
|
Description
|
public
|
Signifies that the member is accessible from outside the class's definition and hierarchy of derived classes.
|
protected
|
The member is not visible outside the class and can be accessed by derived classes only.
|
private
|
The member cannot be accessed outside the scope of the defining class. Therefore, not even derived classes have access to these members.
|
internal
|
The member is visible only within the current compilation unit. The internal access modifier creates a hybrid of public and protected accessibility depending on where the code resides.
|
Note that unless you want a member to have the default access modifier of private, you must specify an access modifier for the member. This is in contrast to C++ where a member not explicitly decorated with an access modifier takes on the visibility characteristics of the previously stated access modifier. For example, in the following C++ code, the members a, b, and c are defined with public visibility, and the members d and e are defined as protected members: -
class CAccessModsInCpp { public: int a; int b; int c; protected: int d; int e; }
To accomplish the same goal in C#, this code would have to be changed to the following: -
class AccessModsInCSharp { public int a; public int a; public int a; protected int d; protected int d; }
The following C# code results in the member b being declared as private: -
public MoreAccessModsInCSharp { public int a; int b; }