Cainiao: constructor and destructor in C #

Source: Internet
Author: User

This section describes how to create, initialize, and destroy an object. This section describes the following topics:

L class Constructor

L structure Constructor

L destructor

 

Class Constructor

This section describes three types of constructor:

Class constructor type

Note

Instance

The instance used to create and initialize the class.

Private

Special instance constructors that are not accessible outside of the class. Classes cannot be instantiated using private constructors.

Static

This constructor is automatically called to initialize the class before the first instance is created or any static member is referenced. You cannot directly call this constructor.

Remarks

(1) instanceConstructor

Class constructor is called when a new object is created, for example:

Point myPoint = new Point ();

A class can have multiple constructors. For example, you can declare a constructor without parameters (such as Point () and a constructor with parameters (such as Point (int x, int y )).

If the class has no constructor, a default non-parameter constructor is automatically generated and the default value is used to initialize the object field (for example, int is initialized to 0 ).

Class constructor can call the base class constructor by using the base keyword. For example:

Public Cylinder (double radius, double height): base (radius, height)

{

}

The class constructor can also call another constructor of the same class by using the keyword "this". For example:

Public Point (): this (0, 20)

{

}

In the previous example, no parameter constructor Point () calls another constructor with two parameters and initializes the default position to (0, 20 ).

 

(2)Private Constructor

A private constructor is a special instance constructor. It is usually used in a class that only contains static members. If a class has one or more private constructors without a public constructor, other classes (except Nested classes) are not allowed to create instances of the class. For example:

Class NLog

{

// Private Constructor:

Private NLog (){}

Publicstatic double e = 2.71828;

}

Declare an empty constructor to Prevent Automatic Generation of default constructor. Note: if you do not use an access modifier for the constructor, it is still a private constructor by default. However, the private modifier is explicitly used to explicitly indicate that the class cannot be instantiated.

 

(3)Static Constructor

Static constructors are used to initialize classes. Before creating the first instance or referencing any static member, the class is automatically initialized by calling the static constructor.

The static constructor neither has an access modifier nor a parameter.

Before creating the first instance or referencing any static member (method or field), the static constructor is automatically called to initialize the class.

Static constructor cannot be called directly. The static constructor cannot access non-static members. It is called only once.

In a program, you cannot control when to execute static constructors.

A typical use of a static constructor is to use this constructor to write entries to a log file when the class uses a log file.

 

Structure Constructor

The structure constructor is similar to the class constructor, but there are the following differences:

The n structure cannot contain explicit non-parameter constructors. Structure members are automatically initialized to their default values.

The n structure cannot have any of the following initial values: base (argument-list ).

The structure can declare constructors, but they must contain parameters. The default (No parameter) constructor of the declared structure is incorrect. The structure member cannot have an initial value. The default constructor is always provided to initialize the structure members as their default values.

For structures, there is no inheritance like classes. A structure cannot inherit from another structure or class. The structure cannot have any of the following initial values: base (argument-list ).

 

Destructor

Destructor are used to destroy class instances.

Remarks

You cannot use destructor for structures. Only destructor can be used for classes.

A class can have only one destructor.

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.