C # static Constructor

Source: Internet
Author: User

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

• If a static constructor is not compiled and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.

• When creating the first class instance or any static member is referenced ,. net will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.

• If the class contains the main method used to start execution, the static constructor of the class will be executed before the main method is called.

• If the static field in the class carries initialization, the initialization statement of the static field runs before the static constructor.

• A class can have only one static constructor.

• A non-parameter constructor can coexist with a static constructor. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.

• Run only once at most.

• Static constructors cannot be inherited

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.