Example Analysis of C Static Constructor Usage

  • 2021-07-01 08:06:33
  • OfStack

This article illustrates the use of C # static constructors. Share it for your reference. The details are as follows:

When we want to initialize some static variables, we need to use static constructors. This static constructor belongs to a class, not an instance, which means that the constructor is executed only once, i.e. automatically called by. NET before creating the first instance or referencing any static members.

Now I come across a scenario where a static method is provided, which is used in different places, involving a parameter with different values, and everything else is completely identical. If you copy the contents of a static method into another method, there is too much code redundancy, so it is not advisable. Using static constructors to handle static variables can simplify the code as much as possible. The following example:


/// <summary>
///  Base class 
/// </summary>
public class A
{
  public static string Field = "original-test";
  // Static constructor 
  static A()
  {
    Field = "test-a";
  }
  public static void Test()
  {
    Console.WriteLine("the output is : " + Field);
  }
}
/// <summary>
///  Subclass 
/// </summary>
public class B : A
{
  // Static constructor 
  static B()
  {
    Field = "test-b";
  }
  public static new void Test()
  {
    A.Test();
    // Call the base class method, which calls the base class Test Function coverage is very important. Without this, then call B.Test() Is actually calling the base class's Test Method. 
  }
}

As the code shows, the type B inherits from the base type A. The static constructor in B assigns a value to the static variable Field, which will be called before calling the Test method in the subclass, while the implementation of the Test method in B completely calls the Test method of the base class A, so that when the method is executed, the value of the static variable Field used in the method is test-b after being assigned in the static constructor in B.

The result of the call is as follows:


static void Main(string[] args)
{
  A.Test();// Output the output is : test-a
  B.Test();// Output the output is : test-b
  Console.ReadLine();
}

A. Test () call, Field value is test-a; And B. Test () call, Field value is test-b.

In this way, when the implementation logic of static methods is complex, you can simplify the code when you need to personalize this method: the subclass re-assigns static variables in the static constructor, and then re-implements the static methods in the base class.

(Note that it is necessary to re-implement the static method in the base class later, otherwise, when you output B. Test (), you call the Test () method of the base class and use the Field variable that is a variable in the base class, and the output becomes as follows:)


static void Main(string[] args)
{
  A.Test();// Output the output is : test-a
  B.Test();// Output the output is : test-a . 
  // Because the subclass method is not overridden, it is equivalent to A.Test()
  Console.ReadLine();
}

I hope this article is helpful to everyone's C # programming.


Related articles: