C # core Foundation-class constructor,

Source: Internet
Author: User

C # core Foundation-class constructor,
I. Constructor

The constructor of a class is a member method of the class. It serves to initialize the members of the class. Class constructor methods include:

1. Static Constructor
2. instance Construction Method

1. Static Constructor

The static constructor of a class is a member method of the class. It is used to initialize static members of the class. See the code example below:

1 using System; 2 namespace LycheeTest {3 class Test {4 // defines a static member variable 5 private static int a; 6 // defines the static constructor 7 static Test () {8 // initialize static member variable 9 a = 11; 10} 11 public void Show () {12 Console. writeLine ("the value of static field a is: {0}", a); 13 14} 15} 16 class Program {17 static void Main (string [] args) {18 Test t = new Test (); 19 t. show (); 20 Console. readKey (); 21} 22} 23}

First, the above Code defines two classes. The Code in line 3rd defines the class Test. When defining a class, there are two access permission modifiers: public and internal. When no access modifier is written, the access permission of the class is internal by default. This access permission means that this class can only be accessed by this Assembly and cannot be accessed by classes other than this Assembly. If this class belongs to the class library, it must be public; otherwise, the Assembly that calls it cannot access it. Line 3 defines a static field member, and line 3 is the static construction method. It can be seen that the static constructor is characterized by the static keyword indicating that the method is static, and the method name must be exactly the same as the class name. Note the case sensitivity here.Static StructureThe creation method cannot contain parameters, and the static construction method cannot return values.. You can initialize static members in the static constructor body. The Code in line 11th defines an instance method, which is used to output the value of a static field. This class is called in the Main (Note: It is main in java) method in the class Program of row 16th, and an instance of this class is created in row 18th, row 3 calls the instance method of the class.

The code above shows that the static constructor of the class is not explicitly called.

The execution result of the above Code is as follows:

The value of static field a is: 11

We can see that the static constructor is indeed executed. The preceding example is one of the execution conditions of the static constructor. When a class instance is created, the static constructor of the class is automatically called.

The call order of the static constructor is after the initial value of the static field.

That is, the first step is to set the default value of the static field. The second step is to set the initial value of the static field, and the third step is to call the static constructor of the class.

Modify the previous code example as follows:

1 using System; 2 namespace LycheeTest {3 class Test {4 private static int a; 5 static Test () {6 a ++; 7} 8 public void Show () {9 Console. writeLine ("the value of static field a is: {0}", a); 10 1411} 12} 13 class Program {14 static void Main (string [] args) {15 Test t = new Test (); 16 t. show (); 17 Test t1 = new Test (); 18 t. show (); 19 Console. readKey (); 20} 21} 22}

This code modifies the static constructor and auto-increment the static field a in the method body. Then, an instance of the class is created in line 2 of the Code, and the instance method of the class is called again.

The execution result is as follows:

The value of static field a is: 1 the value of static field a is: 1

As you can see, the static field value is not added. This is the characteristic of static constructor execution. It is executed only once. When the Assembly runs, an application domain is created. In an application domain, the static constructor of the class is executed only once.

Next, modify the code instance as follows:

1 using System; 2 namespace LycheeTest {3 class Test {4 public static int a; 5 static Test () {6 Console. writeLine ("class static constructor start execution"); 7 a ++; 8} 9 public void Show () {10 Console. writeLine ("the value of static field a is: {0}", a); 11} 12} 13 class Program {14 static void Main (string [] args) {15 Console. writeLine ("the value of static field a is: {0}", Test. a); 16 Console. writeLine ("the value of static field a is: {0}", Test. a); 17 Console. readKey (); 18} 19} 20}

This code prints a line of mark in the static Construction Method of the class, and changes the access permission of the static field of the class to public, so that it can be called outside the class. The value of the static field is printed twice in the Main method. Note that the static field of the call class outside the class must be referenced by the class name.

The following is the code execution result:

Class static constructor start to execute the value of static field a is: 1 the value of static field a is: 1

This Code does not create a class instance. Before a static member of a class is referenced, the static constructor of the class is called. Static members of the called class include static fields and static methods. This is the second condition for calling the static constructor of the class.

Next, modify the code instance as follows:

1 using System; 2 namespace LycheeTest {3 class Program {4 private static int a; 5 static Program () {6 Console. writeLine ("the static Construction Method of the class is called"); 7 a = 11; 8} 9 static void Main (string [] args) {10 Console. writeLine ("the Main method is called"); 11 Console. writeLine ("the value of static field a is: {0}", a); 12 Console. readKey (); 13} 14} 15}

This Code defines static fields and static construction methods in the class containing the Main method. Because the Main method is also a static method, the static constructor of the class is called and it is the entry point method of the class, who calls it first between it and the static constructor of the class? The following describes the code execution result:

The static constructor of the class is called. The value of static field a is: 11.

The code execution result shows that, because the entry point method of the class is still a static method, the static constructor is called first before any static member is called. Therefore, we can draw the following conclusion that the static constructor of the class is called before the Main method of the class.

Can the static constructor of the class be explicitly called? See the code example below:

1 using System; 2 namespace LycheeTest {3 class Program {4 private static int a; 5 static Program () {6 Console. writeLine ("the static Construction Method of the class is called"); 7 a = 11; 8} 9 static void Main (string [] args) {10 Program (); 11 Console. readKey (); 12} 13} 14}

Line 10th in this Code explicitly calls the static constructor of the class, and the compiler reports an error.

2. instance Constructor

The instance constructor of a class is a member method of the class. It serves to initialize the instance members of the class. The instance constructor can implement overloading. When creating an instance of the class, you can explicitly specify different parameters to call different instance constructor methods of overloading. See the code example below:

1 using System; 2 namespace LycheeTest {3 class Program {4 private static int a; 5 private int B = 12; 6 private string c = "Hello World "; 7 8 static Program () {9 Console. writeLine ("the static Construction Method of the class is called"); 10 a = 11; 11} 12 public Program (int a, string s) {13 Console. writeLine ("the constructor with two parameters is called"); 14 this. B = a; 15 this. c = s; 16} 17 public Program (int a): this (a, "Call the constructor using the this keyword") {18 Console. writeLine ("the constructor with a parameter is called"); 19} 20 public void Show () {21 Console. writeLine ("the value of static field a is: {0}", a); 22 Console. writeLine ("the value of instance field B is: {0}", B); 23 Console. writeLine ("the value of instance field c is: {0}", c); 24} 25 static void Main (string [] args) {26 Program p1 = new Program (33, "This is the created instance P1"); 27 Program p2 = new Program (34); 28 p1.Show (); 29 p2.Show (); 30 Console. readKey (); 31} 32}

Lines 4th, 5th, and 6th of this Code respectively define three field members. Line 4th is a static field, and lines 5th and 6th have an initial value. Line 1 of the Code defines an instance constructor. The instance constructor uses the class name as the method name. It does not return a value. Before the method name, it is an access permission modifier, the access permission modifiers that can be used include public, private, and protected. The protected indicates that the constructor can only be accessed within this class. The instance constructor can contain parameters. The instance constructor of the 12th line code uses two input parameters to assign values to the instance fields. The 17th line of code defines an instance constructor with a parameter, which is overloaded with the previous instance constructor. The instance constructor can call other instance constructor using the this keyword. The method is to use a colon after the parameter list, then the this keyword, and then the parameter list, this parameter list must match another overloaded instance constructor. The constructor of row 17th has only one parameter. It passes this parameter to another constructor through the this keyword. When this is used to call another constructor, A string parameter is input for the parameter. The instance method of row 24th prints the value of the field member of the class. In the Main method, the second line of code and the second line of code define two instances respectively. They use the new keyword to call different instance constructor methods. Rows 28th and 29th call the instance method to print the static fields of the class and the values of the members of the two fields of the instance.

The following describes the code execution result:

The class static constructor is called. A constructor with two parameters is called. A constructor with one parameter is called.
The value of static field a is: 11
The value of instance field B is: 33 the value of instance field c is: This is the value of the created instance P1 static field a is: 11 the value of instance field B is: 34. The value of instance field c is: Use the this keyword to call the constructor.

Now we use the execution results to introduce the execution process of the Instance constructor method. When the 26th-line code creates a class instance, the static field of the class is first set to the default value, because there is no field's Initial Value Setting item, the static constructor of the execution class is followed. In this case, static field a is set to 11. Because the 26th line of code uses new to call the instance constructor with two parameters, instance field B is first set to 0, and instance field c is set to null. Then execute the field's Initial Value Setting item. B is assigned 12 values, and c is assigned "Hello World ". Then run the first statement in the instance constructor body. The string "the constructor with two parameters is called" is printed. Next, field B of instance p1 is set to the input parameter 33. Note that the parameter a of the constructor overwrites the static field a of the class. That is to say, the local variable a of the Instance constructor is used. Then, the instance field c is set to the string "this is the created instance P1 ". The Code in line 27th uses the new Keyword to call the instance construction method with a parameter. During the call, instance field B of p2 is set to 0, and instance field c is set to null. Then execute the field's Initial Value Setting item. B is assigned 12 values, and c is assigned "Hello World ". Next we will execute the instance construction method referenced by this with two parameters. "The construction method with two parameters is called" the string is printed. Then, B is set to 34, and c is set to "Call the constructor using the this keyword ". Finally, the code control is returned to execute the print statement in the constructor body of the instance with a parameter. The string "constructor with a parameter is called" is printed. So far, the instance constructor has been executed. The following code prints the static field values. We can see that the static field values printed by the two instances are the same, but their actual field values are different.

Optional parameters and naming parameters can also be used for instance constructor. See the code example below:

1 using System; 2 namespace LycheeTest {3 class Program {4 private int B; 5 private string c; 6 public Program (int a = 12, string s = "") {7 this. B = a; 8 this. c = s; 9} 10 public void Show () {11 Console. writeLine ("the value of instance field B is: {0}", B); 12 Console. writeLine ("the value of instance field c is: {0}", c); 13} 14 static void Main (string [] args) {15 Program p1 = new Program (); // both parameters of the constructor adopt the default value 16 Program p2 = new Program (34); // The default value 17 Program p3 = new Program (23, "Hello World"); // the two parameters of the constructor adopt the input parameter 18 Program p4 = new Program (s: "Today's weather is good"); // use the named parameter, another parameter a uses the default value 19 p1.Show (); 20 p2.Show (); 21 p3.Show (); 22 p4.Show (); 23 Console. readKey (); 24} 25} 26}

Line 6th of the Code defines a constructor with optional parameters and named parameters, and then 15th creates an instance of a class without passing any parameters in the constructor, both parameters of the constructor adopt the default value. Line 1 of code passes in an int type parameter for the constructor. In this case, another string type parameter uses the default value. Two parameters are passed in the 17th line of code. Both parameters of the constructor use these two parameters. The Code in line 18th uses the name parameter to specify that the input parameter is a string type parameter and passes it to the form parameter s. In this case, the other int type parameter uses the default value. The value of the Instance field of the 19th-23rd line code printing class. The execution result of this Code is as follows:

 

The value of instance field B is 12. The value of instance field c is: the value of instance field B is: 34. The value of instance field c is: the value of instance field B is: 23 The value of instance field c is: Hello World the value of instance field B is: 12 the value of instance field c is: today's weather is really good

 

 

 

 

 

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.