Static constructors are a new feature of C#, and they seem to be rarely used. However, when we want to initialize some static variables, we need to use it. This constructor belongs to the class, not to the 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.
class SimpleClass
{
// Static constructor
static SimpleClass()
{
//
}
}
Some points should be paid attention to when using static constructors:
1. The static constructor has neither access modifier nor parameters. Because it is called by .NET, modifiers like public and private have no meaning.
2. When creating the first class instance or any static member is referenced, .NET will automatically call the static constructor to initialize the class, which means that we cannot directly call the static constructor, and we cannot control when the static constructor is executed.
3. A class can only have one static constructor.
4. A constructor without parameters can coexist with a static constructor. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there will be no conflict.
5. Only run once at most.
6. Static constructors cannot be inherited.
7. If the static constructor is not written and the class contains static members with initial value settings, the compiler will automatically generate the default static constructor.
Interesting talk about static constructors!
The static constructor of a class is also called a type constructor, a static constructor. The time it calls is controlled by the CLR:
CLR will select one of the following times to call the static constructor:
1. Before the first instance of the type is created, or before the first access of the type is a non-inherited field or member. The "before" here represents the connection between front and back. The moments here are accurate!
2. At a certain moment before the first access of a non-inherited static field or member, the specific moment is uncertain!
Since the call time is uncertain, it is best not to write code that depends on the execution order of a specific static constructor, which can easily have unpredictable consequences!
Let’s take a look at the following demos below. Let’s take a closer look at some interesting behaviors of static constructors:
Demo1:
static void Main(string[] args)
{
();
}
public class A
{
public static string strText;
static A()
{
strText = "aaaa";
}
}
public class B : A
{
static B()
{
strText = "bbbb";
}
}
Guess what the result is. Some people may think that the output is bbbb, because access requires calling static B() of class B. In fact, the output result is aaaa, because strText is a static field of class A, and class B just inherits this field, so the static constructor static A() of class A will be called here, so the output result is aaaa. There is nothing really to say about this, I believe everyone can see this result.
Let's take a look at the second demo:
Demo2:
static void Main(string[] args)
{
B b = new B();
A a = new A();
();
}
public class A
{
public static string strText;
static A()
{
strText = "aaaa";
}
}
public class B : A
{
static B()
{
strText = "bbbb";
}
}
Guess what the output result is. Some people may think that aaaa will be output. The reason is that new B() will call static B() before, and then new A() will need to call static A before, so the result is aaaa, but the actual situation is not if, the correct result is bbbb, the reason is as follows:
Before executing new B();, the static constructor of class B will be called, that is, it will be called:
static B()
{
strText="bbbb";
}
When executing strText = "bbbb", you need to access the strText field, and the strText field of B is inherited from Class A, so you need to call it first:
static A()
{
strText="aaaa";
}
After executing this function, the value of strText is aaaa
Then the code returns to static B(), and then the line strText="bbbb" in static B() is executed, so the value of strText at this time is bbbbb
When A a=new A(); is executed, the static constructor of A will not be called because it has been called before, and the static function will only be called once in the entire life cycle of the application domain!
Please give me some advice!