C #: access modifier

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.