C # access Modifiers

Source: Internet
Author: User

A recent project compilation encountered a problem: an interface could not be accessed. The view code discovers that the interface definition does not show the modifier, plus the public compiles by playing. Check online for review basics, personally verify: Define a class, and the modifier is praivate or protected

Private Class Testaccess

Compile error: The element defined in the namespace cannot be declared as private, protected, protected internal

access Modifiers total 5 categories: public-public, Internal-Internal, private-private, protected-protected, protected internal. can be in

The elements defined in the namespace are: class, struct (struct), delegate (delegate), Interface (interface), enumeration (enum).

1. Default access modifiers for elements under namespaces

The default access modifier for elements under namespaces is Internal, and only two access modifiers are used under the namespace public and internal

Public: (unrestricted access) any other code of the same assembly or other assemblies that reference the assembly can access the type or member.

internal : (Access is limited to the current assembly) any code in the same assembly can access the type or member, but no other assembly can access it.

2. Default access modifiers for members in each type

The remaining modifiers are primarily positive to inherit the language attribute, with inherited types having two classes (class) and Interfaces (interface). Public,internal can also be used for type members.

Private: (Access is limited to the current class) code for the same class and struct can access that type and member.

Protected: (Access is limited to the current class or class derived from the containing class) code in the same class and derived (inherited attribute) class can access the type and member.

protected internal : (Access is limited to the current assembly or class derived from the containing class) any code or other assembly in the same assembly any derived class

You can access the type or member.

Interface (interface)

The interface member access adornment defaults is considered public and cannot be displayed using the access modifier.

Classes (Class)

The constructor defaults to the public access modifier.

Destructors cannot be displayed using the access adornment Fu Gingme to consider the private access modifier.

The default access modifier for a member of a class is private;

Enum (enum)

Enumeration type members default to the public access modifier and cannot be displayed using modifiers.

Structure (struct)

The struct member defaults to the private modifier.

Struct members cannot be declared as protected members, because structs do not support inheritance.

Nested types

The default access modifier for a nested type is private. and classes, members of the struct have the same default access type.

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 directly contained type.

However, the accessibility domain of a nested type cannot exceed the accessibility domain of the type that contains it.

C # access Modifiers

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.