C # static function

xiaoxiao2021-04-10  556

Using system; class a {public static int x; static a () {x = by 1;}} Class B {public static int y = ax 1; static b () {} static void main () {Console. WriteLine ("x = {0}, y = {1}", AX, by);}}

X = 1, y = 2 To perform the main method, the system must first run the initial value setting item of B.Y before running the static constructor of class B. Since the value of A.x is referenced, the initial value setting item of Y causes the static constructor running A. Thus, the static constructor of A will continue to calculate the value of X to obtain the default value of Y, and A.x is initialized to 1. This completes the process of running a static field initial value setting item and a static constructor, and the calculation of the initial value returns to Y, the calculation result becomes 2.

转载请注明原文地址:https://www.9cbs.com/read-133358.html

New Post(0)