C # execution sequence of Constructor
<1>
Using System; using System. collections. generic; using System. linq; using System. text; execution sequence of the namespace constructor {public class MyBaseClass {public MyBaseClass () {Console. write ("calling constructors without parameters in the parent class");} public MyBaseClass (int I) {Console. write ("constructor that calls a parameter of the parent class") ;}} public class MyDerivedClass: MyBaseClass {public int age; public static int age2; // as long as there is a static variable in the class. Static variables are always initialized first. Static MyDerivedClass () // to initialize static variables, call static constructors. {Age2= 100; Console. write (age2);} public MyDerivedClass (): this (5) {age = 101; Console. writeLine (age);} public MyDerivedClass (int I) {age = 102; Console. writeLine (age);} public MyDerivedClass (int I, int j) {Console. writeLine ("parameters of two variables");} class Program {static void Main (string [] args) {MyDerivedClass myder = new MyDerivedClass (); // output 100, "Calling a constructor without parameters in the parent class", 101 // execution sequence: <1>: static MyDerivedC Lass () <2>: public MyBaseClass () <3>: public MyDerivedClass () // define MyDerivedClass myder2 = new MyDerivedClass (5 ); // output "calling constructors without parameters in the parent class", 102. // The static variable age2 has been initialized when the myder object is initialized. Because the static constructor can be executed at most once, the static variable age2 will not be initialized when the myder2 object is initialized. // The execution sequence is: <1>: public MyBaseClass () <2>: public MyDerivedClass (int I) // What if I want to call a constructor with a parameter in the parent class when initializing the myder2 object? It is easy to add a base (I)/* base keyword to the constructor of the derived class to access the members of the base class from the derived class; specify the base class constructor to call when creating a derived class instance. Public MyDerivedClass (int I): base (I) {age = 102; Console. writeLine (age);} execution sequence: <1>: public MyBaseClass (int I) <2>: public MyDerivedClass (int I) here, the output is "calling the constructor of a parameter of the parent class", 102 * // using MyDerivedClass myder3 = new MyDerivedClass (5, 6 ); // output "calling constructors without parameters in the parent class", "parameters of two variables" // execution sequence: <1>: public MyBaseClass () <2>: public MyDerivedClass (int I, int j) Console. readKey ();}}}
<2>
Using System; using System. collections. generic; using System. linq; using System. text; execution sequence of the namespace constructor {public class MyBaseClass {public MyBaseClass () {Console. write ("calling constructors without parameters in the parent class");} public MyBaseClass (int x) {Console. write ("constructor that calls a parameter of the parent class") ;}} public class MyDerivedClass: MyBaseClass {public int age; public static int age2; // as long as there is a static variable in the class. Static variables are always initialized first. Static MyDerivedClass () // to initialize static variables, call static constructors. {Age2= 100; Console. write (age2);} public MyDerivedClass (): this (5) {age = 101; Console. writeLine (age);} public MyDerivedClass (int I): base (I) {age = 102; Console. writeLine (age);} public MyDerivedClass (int I, int j) {Console. writeLine ("parameters of two variables");} class Program {static void Main (string [] args) {MyDerivedClass myder = new MyDerivedClass (); // output 100, "The constructor that calls a parameter of the parent class", 102,101 // execution sequence: <1>: stat Ic MyDerivedClass () <2>: public MyBaseClass (int x) <3>: public MyDerivedClass (int I) <4>: public MyDerivedClass () // future // MyDerivedClass myder2 = new MyDerivedClass (5); // output "calling constructors without parameters of the parent class", 102. // The static variable age2 has been initialized when the myder object is initialized. Because the static constructor can be executed at most once, the static variable age2 will not be initialized when the myder2 object is initialized. // The execution sequence is: <1>: public MyBaseClass () <2>: public MyDerivedClass (int I) // What if I want to call a constructor with a parameter in the parent class when initializing the myder2 object? It is easy to add a base (I)/* base keyword to the constructor + function of the derived class to access the members of the base class from the derived class; specify the base class constructor to call when creating a derived class instance. Public MyDerivedClass (int I): base (I) {age = 102; Console. writeLine (age);} execution sequence: <1>: public MyBaseClass (int I) <2>: public MyDerivedClass (int I) here, the output is "calling the constructor of a parameter of the parent class", 102 * // else // MyDerivedClass myder3 = new MyDerivedClass (5, 6 ); // output "calling constructors without parameters in the parent class", "parameters of two variables" // execution sequence: <1>: public MyBaseClass () <2>: public MyDerivedClass (int I, int j) Console. readKey ();}}}