Access modifiers for C # classes, methods

Source: Internet
Author: User
Tags class definition modifiers

This article mainly introduces the use of C # class access modifier, a more detailed analysis of C # class access modifier concept and usage, with a certain reference value, the need for friends can refer to the following

In this paper, the use of access modifiers for C # classes is analyzed in detail and shared for everyone's reference. The specific usage analysis is as follows:

By default, a class is declared internal, which means that only code in the current project can access it. You can explicitly specify it with the internal access modifier keyword, but this is not required and the class is defined by default for this type of class. But the C # method default Access level: Private.

The access level of a method or property's modifier is as follows:

Access modifiers

Significance

Public

Access is unrestricted and can be accessed within the class and in code outside of any class

Protected

An accessible domain is scoped to or within a class that derives from that class

Internal

Accessible domains are scoped to the assembly in which the class resides

protected internal

Protected or internal, accessible domains are scoped to the program in which the class resides or to those classes that derive from the class to which it belongs

Private

An accessible domain is limited to the class it belongs to

Combination of access modifiers that can be used in a class definition

None or internal class can only be accessed in the current project
The public class can be accessed from anywhere
Abstract or internal abstract classes can only be accessed in the current project, cannot be instantiated, only inherit
The public abstract class can be accessed anywhere, cannot be instantiated, and can inherit only
Sealed or internal sealed class can only be accessed in the current project, cannot be derived, can only be instantiated
The public sealed class can be accessed anywhere, cannot be derived, and can only be instantiated

The default access modifiers for methods, classes, and so on in C # are described below

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 class, the members of the struct have the same type of default access

I hope this article is helpful to everyone's C # programming.

Source: http://www.jb51.net/article/56597.htm

Access modifiers for C # classes, methods

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.