Static constructor in C #

Source: Internet
Author: User
(1) It is used to initialize static and read-only fields. (2) You cannot add access modifiers when adding static keywords because static constructors are private. (3) Class static constructor in a given application Program Execute at most once in a domain: only the instance of the class or any static member of the referenced class can stimulate the static Constructor (4). Static constructor cannot be inherited and cannot be called directly. (5) If the class contains the main method used to start execution, the static constructor of the class will be executed before calling the main method. For any static field with an initial value, the initial values must be executed in the text order before executing the static constructor of this class. (6) 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. Example Code Further description: /*************************************** * *********** Study on the number of Statically constructed letters * (1) ① ③ ...... For execution order * (2) output result: static A () * Static B () * x = 1, y = 2 ************************************* * *************/using system; class A {public static int X; static A () // ④ After execution, return to ③ {x = B .y + 1; console. writeline ("static A ()") ;}} Class B {public static int y = a.x + 1; // ③ call the static member of, // go to the static constructor of Class A ----> static B () // ② if there is a static field with an initial value, // when executing the static constructor of this class, // execute the initial values in the text order first. // Go to the initial value setting item ----> {console. writeline ("static B ()");} static void main () // ① program entry, // if the class contains the main method used to start execution, // The static constructor of this class will be executed before the main method is called. // Go to the static constructor of B ----> {console. writeline ("x = {0}, y = {1}", a.x, B .y); // ⑤ output result console. readline ();}}

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.