C# Classes Explained - Access Modifiers
(Page 2 of 6 )
You can control the visibility of class members for the other classes and other code using Access Modifier keywords: public, private, protected and internal. In C#, you must explicitly indicate the access modifiers for each member of you class, except from the private members, as we have observed. The next table discusses the available Access Modifiers:
Access Modifier | Description |
| public | States the the class member accessible for all the derived classes, classes in other assemblies and for any other code that want to you it. |
| private | It's the opposite of the public Access Modifier, states that the class member is not accessible for any code including derived classes so the only code that can access (or manipulate) this member is the declared class. Actually Object oriented Programming provide data protection using the private (and protected) Access Modifiers. For example, you can declare the salary field inside the Employee class as private and it will not be accessible and it will not be visible outside this class declaration so there's no client code that can modify its value unless you the programmer want that. |
| protected | This is an intermediate access level between the public and the private Modifiers. It states that the member is accessible for only derived classes which gives you the ability to declare fields that are not accessible outside the declared class and any class that derive it. For example, you can define a class like ManagerEmployee that derive and extend the Employee class. |
| internal | This is an interesting Access Modifier that states that the class member is accessible only for the current assembly (we will have our discussion about assemblies later). This means that the member is public for the assembly but it's not visible outside the assembly. |
Next: The this keyword >>
More C# Articles
More By Michael Youssef