Access Modifiers
Learn to control the visibility of methods and other class members.
All class members have access modifiers. We already encountered them when we decorated our classes with the public keyword.
Access modifiers define a context in which a given class or its member can be used.
- The
publicmodifier makes aclassor its member public to all external code. They can be accessed from any other part of the program and even from a different program or assembly. - The
privatemodifier makes aclassor its member closed to the external environment. A privateclasscan only be accessed from theclasswhere it’s defined. We can’t make aclassprivate on thenamespacelevel. Members that areprivatecan only be accessed from within theclasswhere they’re defined. - The
protectedmodifier can only be used withclassmembers. These members are only accessible from the sameclassor any derivedclass. - With the
internalmodifier,classesand members are only visible inside the assembly where they’re defined.
Note: An ...
Ask