What are Access Modifiers in C#?

Access modifiers are keywords used to specify the declared accessibility of a member or a type. An access specifier precedes the rest of a members type specification.

Public – When a member of a class is modified by the public specifier, that member can be freely accessed by code defined outside of its class. It marks a member as accessible from an object variable as well as any derived classes.

Private – When a member of a class is specified as private, that member can be accessed only by other members (methods) defined by its class. In C#, all members are private by default (if no access specifier is used, a class member is private by default). Enum and interface are public by default.

Protected – When a member of a class is specified as protected, that member is private to their class, but still can be inherited and accessed by a derived class. Protected methods, however, are not accessible from an object variable.

Internal – The internal modifier declares that a member is accessible throughout all files (by any type) in an assembly, but not outside the assembly. In simplified terms, a member marked internal is known throughout a program, but not elsewhere.

The internal modifier can be applied to classes and members of a class, structures and members of structures, interface and enumeration declarations.

Protected Internal – A member declared protected internal access is accessible within its own assembly or types derived from the defining class in the current assembly. The protected internal access level can be given only to class members.

No comments:

Post a Comment