Chapter 2 C # basic learning 2

Source: Internet
Author: User

2.2.5.4 Constructor

A constructor is a special method that calls it to initialize an object. The name of the constructor is always the same as the name of the class containing it and never returns a value. Constructor can be divided into the following two types:

1. The instance constructor is used to initialize a specific object instance.

2. Static constructor is also called before the first class.

Base class initialization

The constructor of the subclass calls the constructor of the base class. C # The Compiler inserts a call to the base class before executing any code in the constructor of the current class.

An overloaded constructor does not automatically search for base class constructor that matches the corresponding parameter list, but implicitly calls the default base class constructor.

Member Initial Value Setting

A value assignment expression is provided when a member variable is declared.

Static members can also be initialized as powerful members, but their initialization process should be performed before any force members are initialized. Correct initialization order:

1. Initialize all static fields.

2. Call the static constructor.

3. Change all instance fields.

4. Call the instance constructor.

Static Constructor

The static constructor is automatically called before the class instance is created.

2.2.5.5 destructor

Destructor is an optional member function, including the Code required to destroy an instance of the class.

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.