SoFunction
Updated on 2025-03-01

C# Static Constructor Usage Example Analysis

This article describes the usage 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 a static constructor. This static constructor belongs to a class, not an instance, which means that this constructor will only be executed once, that is, it is automatically called by .NET before creating the first instance or referring to any static member.

Now we encounter a scenario where we provide a static method, which is used in different places, involving different parameter values, and the others are completely consistent. If you copy the content of the static method and make it into another method, the code is too redundant and the method is not advisable. Using static constructors to handle static variables can make the code as simplified 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()
  {
    ("the output is : " + Field);
  }
}
/// <summary>
/// Subclass/// </summary>
public class B : A
{
  // Static constructor  static B()
  {
    Field = "test-b";
  }
  public static new void Test()
  {
    ();
    //Calling the base class method, this is very important to override the base class Test function. Without this, the base class Test method is actually called when calling ().  }
}

As shown in the code, type B inherits from base type A. The static constructor in B assigns the static variable Field, which will be called before calling the Test method in the subclass. The implementation of the Test method in B is to completely call the Test method of the base class A. In this way, when the method is executed, the value of the static variable Field used in the method is the test-b after being assigned in the static constructor of B.

The call result is as follows:

static void Main(string[] args)
{
  ();//The output is: test-a  ();//The output is: test-b  ();
}

The Field value is test-a; and the Field value is test-b when the () call is test-b.

In this way, when the implementation logic of the static method is complex, the code can be simplified when it is required to personalize the implementation of this method: the subclass reassigns static variables in the static constructor, and then re-implements the static method in the base class.

(Note that it is necessary to re-implement the static method in the base class later. Otherwise, when outputting(), the Test() method of the base class is called, and the Field variable used is the variable in the base class. At that time, the output becomes as follows:)

static void Main(string[] args)
{
  ();//The output is: test-a  ();//Output the output is : test-a.  //Because there is no subclass method overriding, it is equivalent to ()  ();
}

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