Detailed explanation of the construction method of c class

  • 2021-11-29 08:17:35
  • OfStack

1. Construction method

Class constructor is one of the member methods of a class, and its function is to initialize the members in the class. Class construction methods are divided into:

1. Static construction method

2. Instance construction method

1. Static construction method

The static constructor of a class is one of the member methods of a class, and its function is to initialize the static members of the class. Look at the code example below:


using System;
namespace LycheeTest {
 class Test {
 // Definition 1 Static member variables 
 private static int a;
 // Defining a static constructor 
 static Test() {
  // Initialize static member variables 
  a = 11;
 }
 public void Show() {
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
 }
 }
 class Program {
 static void Main(string[] args) {
  Test t = new Test();
  t.Show();
  Console.ReadKey();
 }
 }
}

First, the above code defines two classes. Line 3 defines the class Test. When defining a class, there are two access modifiers for the class, one is public and the other is internal. When no access modifiers are written, the access permission of the class defaults to internal. The meaning of this access right is that this class can only be accessed by this assembly, and cannot be accessed by classes outside this assembly. If this class belongs to a class library, it must be public, otherwise the calling assembly cannot access it. Line 5 defines a static field member, and line 7 is the static constructor. As you can see, the characteristic of static constructors is that the static keyword indicates that the method is static, and the method name should be exactly the same as the class name, paying attention to the case here. Static constructors cannot have parameters, and static constructors cannot have return values. You can initialize static members in the static constructor body. Line 11 defines an instance method, which outputs the value of a static field. This class is called in the Main (note: main in java) method of class Program in line 16, an instance of this class is created in line 18, and an instance method of the class is called in line 19.

As you can see from the above code, the static constructor of the class is not explicitly called.

Look at the execution results of the above code below:

静态字段 a 的值是:11

As you can see, the static constructor is indeed executed. Then the above example is the execution condition of the static constructor. When the instance of the class is created, the static constructor of the class will be automatically called.

The invocation order of static constructors is after the initializer of static fields.

That is, step 1 is to set the default value of the static field, step 2 is to execute the initializer of the static field, and step 3 is to call the static constructor of the class.

Let's modify the previous code example by 1, and the code is as follows:


using System;
namespace LycheeTest{
 class Test {
 private static int a;
 static Test() {
  a++;
 }
 public void Show() {
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
  14
 }
 }
 class Program {
 static void Main(string[] args) {
  Test t = new Test();
  t.Show();
  Test t1 = new Test();
  t.Show();
  Console.ReadKey();
 }
 }
}

This code modifies the static constructor, adding the static field a in the method body. Then you create another instance of the class in line 17, and then call the instance method of the class again.

Look at the implementation results below:


 Static field  a  The value of is: 1 
 Static field  a  The value of is: 1

As you can see, the value of the static field does not increase. This is the characteristic of static constructor execution, which only executes once. When the assembly runs, an application domain is created, in which the static constructor of the class executes only once.

Modify the code example as follows:


using System;
namespace LycheeTest {
 class Test {
 public static int a;
 static Test() {
  Console.WriteLine(" The static constructor of the class starts executing ");
  a++;
 }
 public void Show() {
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
 }
 }
 class Program {
 static void Main(string[] args) {
  Console.WriteLine(" Static field  a  The value of is: {0}", Test.a);
  Console.WriteLine(" Static field  a  The value of is: {0}", Test.a);
  Console.ReadKey();
 }
 }
}

This code prints out a line of markup in the static constructor of the class, and the access to the static fields of the class is changed to public, which allows it to be called outside the class. The static field values are printed out twice in the Main method. Note that calling the static field of the class outside the class requires a reference using the class name.

The following is the result of code execution:


 The static constructor of the class starts executing 
 Static field  a  The value of is: 1
 Static field  a  The value of is: 1

This code does not create an instance of the class. Before referencing the static members of the class, the static constructor of the class will be called. The static members of the called class include static fields and static methods. This is the second condition of the static constructor call of the class.

Modify the code example as follows:


using System;
namespace LycheeTest {
 class Program {
 private static int a;
 static Program() {
  Console.WriteLine(" The static constructor of the class is called ");
  a = 11;
 }
 static void Main(string[] args) {
  Console.WriteLine("Main  Method is called ");
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
  Console.ReadKey();
 }
 }
}

This code defines static fields and static constructors in the class that contains 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? Let's first look at the execution results of the code:


 The static constructor of the class is called 
Main  Method is called 
 Static field  a  The value of is: 11

As you can see from the execution of the code, since 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, it can be concluded that the static constructor of the class is called before the Main method of the class.

Can the static constructor of a class be called explicitly? Look at the code example below:


using System;
namespace LycheeTest {
 class Program {
 private static int a;
 static Program() {
  Console.WriteLine(" The static constructor of the class is called ");
  a = 11;
 }
 static void Main(string[] args) {
  Program();
  Console.ReadKey();
 }
 }
}

Line 10 of this code explicitly calls the static constructor of the class, when the compiler reports an error.

2. Instance constructor

Class instance construction method is one of the member methods of a class, and its function is to initialize the instance members of a class. Instance constructors can be overloaded, and when creating an instance of a class, you can explicitly specify different parameters to call different overloaded instance constructors. Look at the code example below:


using System;
namespace LycheeTest {
 class Program {
 private static int a;
 private int b = 12;
 private string c = "Hello World";
 static Program() {
  Console.WriteLine(" The static constructor of the class is called ");
  a = 11;
 }
 public Program(int a, string s) {
  Console.WriteLine(" Belt 2 The constructor of a parameter is called ");
  this.b = a;
  this.c = s;
 }
 public Program(int a) : this(a, " Pass  this  Keyword calls the constructor ") {
  Console.WriteLine(" Belt 1 The constructor of a parameter is called ");
 }
 public void Show() {
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
  Console.WriteLine(" Instance field  b  The value of is: {0}", b);
  Console.WriteLine(" Instance field  c  The value of is: {0}", c);
 }
 static void Main(string[] args) {
  Program p1 = new Program(33, " This is the instance created  P1");
  Program p2 = new Program(34);
  p1.Show();
  p2.Show();
  Console.ReadKey();
 }
 }

Lines 4, 5, and 6 of this code define three field members, respectively, line 4 is a static field, and lines 5 and 6 have initializers. Line 8 of the code is the definition of an instance constructor. The instance constructor also takes the class name as the method name, which has no return value. The access modifiers are preceded by the method name, and the access modifiers that can be used include public, private and protected. protected means that constructors can only be accessed inside this class. Instance constructors can take parameters. The instance constructor in Line 12 assigns a value to the instance field using two passed-in parameters. Line 17 defines an instance constructor with one parameter, which forms an overload with the previous instance constructor. The instance constructor can call other instance constructors through the this keyword by using a colon after the argument list followed by the this keyword followed by the argument list that matches another overloaded instance constructor. The constructor in line 17 has only one argument, which it passes to another constructor via the this keyword, and passes in a string argument for the other constructor when it is called with this. The instance method in line 24 prints the values of the field members of the class. In the Main method, two instances are defined in line 26 and line 27, which use the new keyword to call different instance constructors. Lines 28 and 29 call the instance method to print the values of the static field of the class and the two field members of the instance, respectively.

Let's look at the execution results of the code first:


 The static constructor of the class is called   Belt 2 The constructor of a parameter is called   Belt 2 The constructor of a parameter is called   Belt 1 The constructor of a parameter is called  
 Static field  a  The value of is: 11 
 Instance field  b  The value of is: 33
 Instance field  c  The value of is: This is the instance created  P1  Static field  a  The value of is: 11
 Instance field  b  The value of is: 34
 Instance field  c  The value of is: through  this  Keyword calls the constructor 

Now, when line 26 creates an instance of the class, the static fields of the class are first set to their default values, because there are no initializers for the fields, so the static constructor of the class is then executed. The static field a is set to 11. Because line 26 calls the instance constructor with two parameters using new, the instance field b is set to 0 first, and the instance field c is set to null. The field initializer is then executed, b is assigned to 12, and c is assigned to "Hello World". Next, the first statement in the instance constructor body is executed, and the string "constructor with 2 parameters called" is printed. Next, the field b of the instance p1 is set to the passed-in parameter 33, noting that the formal parameter a of the constructor overrides the static field a of the class here. That is, it is the local variable a of the instance constructor that works. The instance field c is then set to the string "This is the instance created P1". Line 27 calls an instance constructor with one parameter using the new keyword, when the first instance field b belonging to p2 is set to 0, and the instance field c is set to null. The field initializer is then executed, b is assigned to 12, and c is assigned to "Hello World". Next, the instance constructor with two parameters referenced by this is executed, and the string "constructor with two parameters called" is printed. Then b is set to 34, and c is set to "call constructor via this keyword". Finally, code control returns to execute the print statement in the 1-argument instance constructor body, and the string "1-argument constructor called" is printed. At this point, the execution of the instance constructor is complete. The next code prints the static field values, and you can see that the static field values printed by the two instances are the same as 1, but the values of their instance fields are different.

Optional and named parameters can also be used for instance constructors, as shown in the code example below:


using System;
namespace LycheeTest{
 class Test {
 private static int a;
 static Test() {
  a++;
 }
 public void Show() {
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
  14
 }
 }
 class Program {
 static void Main(string[] args) {
  Test t = new Test();
  t.Show();
  Test t1 = new Test();
  t.Show();
  Console.ReadKey();
 }
 }
}
0

Line 6 defines a constructor with optional and named arguments, and Line 15 creates an instance of the class with no arguments passed in the constructor, where both parameters of the constructor take default values. Line 16 passes in a parameter of type int for the constructor, and the other parameter of type string takes the default value. Line 17 passes in two parameters, which are used by both parameters of the constructor. Line 18 specifies that the parameter passed in is of type string using a named parameter and passes it to the formal parameter s. At this time, the other parameter of int type takes the default value. Lines 19 through 23 print the value of the instance field of the class. The execution result of this code is as follows:


using System;
namespace LycheeTest{
 class Test {
 private static int a;
 static Test() {
  a++;
 }
 public void Show() {
  Console.WriteLine(" Static field  a  The value of is: {0}", a);
  14
 }
 }
 class Program {
 static void Main(string[] args) {
  Test t = new Test();
  t.Show();
  Test t1 = new Test();
  t.Show();
  Console.ReadKey();
 }
 }
}
1

Related articles: