C # using the new operator to construct what is done in the new object (simple version)

Source: Internet
Author: User

New is a frequently used keyword.

Here we only describe the role of the new object () type.

Others are not involved at the moment,

The content in this article is also relatively simple, and some common procedures are listed.

 

1. AccessProgram(The corresponding program will be loaded, for example, the new object needs to be in another DLL)

In fact, there are a lot of other content in step 1, but I will not describe it in detail here, because it is rarely used.

If the DLL cannot be found, an exception will be thrown.

2. assign values to all static members (static)

The order of values assigned in C # looks like the sumCodeThe order from top to bottom is the same, but it is not guaranteed that later. Net versions and compilers are the same. Therefore, do not rely on the code sequence.

3. Call the static Constructor (... ctor)

The static constructor is called only once from start to end. If you want to create the singleton mode, it is also OK to initialize it here,

If a static constructor throws an exception, all the members of this class will not be able to access the static constructor during the application's lifecycle. Upon access, the system. typeinitializationexception will be thrown.

Therefore, exercise caution in the static constructor, preferably try catch, if not a very serious error.

The Order relationships mentioned in point 2nd can be executed in the constructor sequentially.

4. if this class has a parent class, call the constructor of the parent class first. (if the parent class has a parent class, so continue to call the parent class of the parent class .......)

All the way to the highest level, if you want to call a specific constructor of the parent class, you can use base (.....)

5. Initialize all instance members

6. If this (...) exists, it means to call other constructors first.

7. start calling the constructor.

Generally, constructors should be lightweight as much as possible,

A lot of details are ignored here, such as what is done in the static Constructor (that is, the Type constructor)

 

PS1: The value type is also acceptableIntI =New Int(); But actually it is no different from int I = 0; of course, the value type you define can be useful for constructors.

PS2: A lot of memory allocation is also omitted here

 

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.