Access modifier (good article, post)

Source: Internet
Author: User

 

C # access modifiers Author: ay2008080808 submission date: 15:51:00 | category: | access traffic: 34

 

An access modifier is a keyword used to specify the accessibility of declared members or types. These keywords include public, private, protected, and internal.

1. Accessibility level (accessibility levels)

Use these access modifiers to specify the following five levels of accessibility (accessibility levels ):
◆ Public access is not restricted.
Access is unrestricted.
◆ Protected access is limited to the containing Class or types derived from the containing class.
The access scope is limited to the class to which it belongs or the type derived from this class.
◆ Internal access is limited to the current Assembly.
The access scope is limited to this Program .
◆ Protected internal access is limited to the current Assembly or types derived from the containing class.
The access scope is limited to this program or the types derived from its classes.
◆ Private access is limited to the containing type.
The access scope is limited to its type.

Only one access modifier is allowed for a member or type, before t for the protected internal combination.

Access modifiers are not allowed on namespaces. namespaces have no access restrictions.

Depending on the context in which a member declaration takes place, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default accessibility is used.

Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.

Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.
----------------------------------------------------------------------
Members of | default member | allowed declared accessibility
| Accessibility | of the member
----------------------------------------------------------------------
Enum | public | none
----------------------------------------------------------------------
Class | private | public
| Protected
| Internal
| Private
| Protected internal
----------------------------------------------------------------------
Interface | public | none
----------------------------------------------------------------------
Struct | private | public
| Internal
| Private
----------------------------------------------------------------------
The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediatcontaining type. however, the Accessibility domain of a nested type cannot exceed that of the containing type.

Ii. Access by Members

The accessible type that can be selected when a Member is declared depends on the context where the member declaration appears. In addition, when a member declaration does not contain any access modifier, the context at which the Declaration occurs selects a default accessibility for the member.

◆ The namespace is implicitly public and declared accessible. Access modifiers are not allowed in the namespace declaration.
The types declared in the compilation unit or namespace can be public or internal declared Accessability. The default declared Accessability is internal.
◆ Class members can have any of the five declared Accessability types. The default value is private. (Note that the type declared as a class member can be any of the five declared Accessability types, the declared namespace member type can only be public or internal declared accessible .)
◆ The structure member can have public, internal, or private declared Accessability. The default value is private and declared Accessability, because the structure is implicitly sealed. If a structure member is declared in this structure (that is, it is not inherited from its base class), it cannot have the declared accessibility of protected or protected internal. (Note that the type declared as a structure member can be public, internal, or private with declared Accessability, the declared namespace member type can only be public or internal declared accessible .)
◆ Interface members are implicitly public and declared accessible. Access modifiers are not allowed in the interface member declaration.
◆ Enumeration members are implicitly public and declared accessible. Access modifiers are not allowed in enumeration member declarations.

 

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.