C # class Explanation

Source: Internet
Author: User

Class:

A class is a data structure that can contain members (constants and fields), function members (methods, properties, events, indexers, operator instances, constructor static constructors, and destructors), and nested types. Class types support inheritance, which is a mechanism that allows derived classes to extend and specialize base classes.

1. class declaration:

Attribute (optional) class modifier (optional) class identifier class base (optional) body ( optional);

2. Class modifier:

Class declarations can include a sequence of class modifiers as needed:

Class-modifier (class modifier):

New

Public

Protected

Internal

Private

Abstract

Sealed

The new modifier applies to nested classes. It indicates that the class being decorated hides the inherited members of the same name.

3. Abstract class:

The abstract: modifier is used to indicate that the class being decorated is incomplete, and that it can only be applied as a base class. The different points of abstract and non-abstract classes are as follows:

Abstract classes cannot be instantiated directly, and using the new operator with an abstract class can cause compile-time errors. Although some variables and values at compile-time types can be abstract, such variables and values must either be null or contain references to instances of non-abstract classes (this non-abstract class derives from an abstract class).

Allow abstract classes to contain abstract members

Abstract classes cannot be sealed

4, Sealed Type:

The sealed modifier is used to prevent other classes from being derived from the decorated class. If a sealed class is specified as the base class for other classes, a compile-time error occurs.

Sealed classes cannot be abstract classes at the same time.

The sealed modifier is primarily used to prevent unintended derivation, but it also enables some runtime optimizations. Specifically, because sealed classes never have any derived classes, calls to virtual function members of an instance of a sealed class can be converted to non-virtual calls to handle.

5. Base class Specification:

A class declaration can contain a base class specification that defines the direct base class of the class and the interfaces implemented by the class.

Class-base: (base class:)

:Class-type (: Class type )

:interface-type-list (: List of interface types )

6. Base class:

When a class type is contained in a class base, it means that the class is the direct base class of the class being declared. If there is no base class in the class declaration, or if the containing class base lists only the interface type, it is assumed that the direct base class is object, and the class inherits the members from its immediate base class.

The direct base class of a class type cannot be of the following types:System.Array, System.Delegate, System.Enum, System.ValueType.

The base class of a class includes its immediate base class and the base class of the direct base class.

In addition to class object, each class has and has only one direct base class. The object class has no direct base class and is the ultimate base class for all other classes.

7, interface implementation:

The class-base specification can contain a list of interface types, which means that the declared class implements the individual interface types listed.

8. Class Body:

The class body is used to define the members of the class.

Class-body: (class body )

{class-member-declarationsopt} ({class member declaration optional })

9. Class Members:

A class member has two parts: a member that has its class member declaration introduced, and a member that inherits from its immediate base class

Class-member-declarations: (class member declaration:)

Constant-declaration: (Constant declaration:)

Field-declaration:

Method-declaration:

Property-declaration:

Event-declaration:

Indexer-declaration:

Operator-declaration:

Constructor-declaration:

Destructor-declaration:

Static-constructor-declaration:

Type-declaration:

C # class Explanation

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.