Read Summary (6) Details on the instance constructor and Type constructor

Source: Internet
Author: User
1. instance Constructor (constructor)

The instance constructor is a special method that allows you to initialize a type of instance to a good state. It is executed when each new instance of the class is created.
The Code is as follows:

Namespace constructordemo1 {internal sealed class myclass // literal class {datetime timeofinstantiation; // declare the field public myclass () // constructor {timeofinstantiation = datetime. now; // initialization field} class program {static void main (string [] ARGs) {myclass MC = new myclass (); // create an object, simultaneous execution class instance constructor }}}

Note: The execution sequence of the constructor is: Member initialization ---> base class constructor call ---> constructor body execution

2. Type constructor

Type constructor, also known as a static constructor. It is used to set the initial status of a type (note that it is not a type instance.
The Code is as follows:

Internal sealed class myclass {Private Static int s_x; // declare static field static myclass () // Type constructor {s_x = 10; // initialize static field }}

Note: the code in the Type constructor can only access static fields of the type, and its common purpose is to initialize these fields.

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.