Default access modifiers for methods and classes in C #

Source: Internet
Author: User

 

C # default access level of the method: private

C # default class access level: internal

 

 

1. default access modifier for elements in the namespace

 

Public: Any other code of the same assembly or other assembly that references the Assembly can access this type or member.

Internal: Any code in the same assembly can access this type or member, but other assembly cannot.

 

2. default access modifiers for members of various types

The remaining modifiers are mainly inherited from this language. The inherited types include class and interface ). Public and internal can also be used for type members.

Private: Code of the same class and structure can access this type and members.

Protected: code in the same class and derived (inherited feature) class can access this type and member.

Protected internal: Any code in the same assembly or any derived class in other assembly can access this type or member.

 

MSDN prompt:

1) The accessibility of a derived class cannot be higher than its base type. In other words, there cannot be A public class B Derived from internal Class. If this is allowed, A will become A public class, because all protected members or internal members of A can be accessed from the derived class.

2) The accessibility of a member must not be higher than that of its contained type.

3) You can declare class members (including Nested classes and structures) using any of the five access types ).

 

Interface)

The default access modifier for interface members is public, and the access modifier cannot be displayed.

 

Class)

The default constructor is a public access modifier.

The Destructor cannot display the access modifier and is a private access modifier by default.

The default access modifier for class members is private;

 

Enumeration (enum)

By default, enumeration type members are public access modifiers and cannot be displayed with modifiers.

 

Structure (struct)

The structure member is a private Modifier by default.

The structure member cannot be declared as a protected member because the structure does not support inheritance.

 

Nested type

The default access modifier of the nested type is private. It is consistent with the default access type of the class and structure members.

From: Andrew's Blog

Related Article

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.