-
Public
-
Any other code in the same assembly or other assembly that references the Assembly can access this type or member.
-
Private
-
Only the code in the same class or structure can access this type or member.
-
Protected
-
Only the code in the same class or structure or derived class can access the type or member.
-
Internal
-
Any code in the same assembly can access this type or member, but the code in other assembly cannot.
-
Protected internal
-
Types or members that can be accessed by any code in the declared assembly or a class derived from another assembly. Access from another assembly must occur in the class declaration. The class declaration is derived from the class that declares protected internal elements and must occur through an instance of the derived class type.
Here, we will mainly talk about protected internal. The access permissions it limits are the union of the access permissions of internal and protected, rather than the intersection. That is to say, its access permissions include the access permissions of internal and
The access permission of protected. You can use the following formula to describe its access permissions:
(Protected internal) access permission = (internal) access permission + (protected) access permission
Or you can understand this.
(Protected internal) access permission = (internal) access permission of principal (protected) access permission
Comparison of access permissions:
Private <protected internal <public or private <internal <protected internal <public
The access permissions of protected and internal are not comparable.
Pay attention to the following points for access modifiers:
① One member or type can only have one access modifier, except when using the protected internal combination.
② Access modifiers are not allowed in the namespace. The namespace has no access restrictions.
③ Only the accessibility of some declarations is allowed Based on the context in which the member declaration appears. If no access modifier is specified in the member declaration, the default accessibility is used. The accessibility of top-level types not nested in other types can only be internal or public. The default accessibility of these types is internal. The nested type is a member of another type, which can have the accessibility declared as shown in the following table.
| Belong |
Default accessible members |
The declared accessibility allowed by this Member |
| Enum |
Public |
None |
Class |
Private |
Public Protected Internal Private Protected internal |
| Interface |
Public |
None |
Struct |
Private |
Public Internal Private |