About C # static Constructor

Source: Internet
Author: User
Description of C # static Constructor

Static constructor is a new feature of C #, which is rarely used. However, we need to use static variables when initializing them. This constructor belongs to a class rather than an instance. That is to say, this ConstructorExecuted only once. That is, creatingFirstAutomatically called by. net before the instance or reference any static members.

Class Simpleclass
{
// Static Constructor
Static Simpleclass ()
{
//
}
}

Pay attention to the following points when using static constructors:

1. The static constructor neither has an access modifier nor a parameter. Because it is called by. net, modifiers such as public and private are meaningless.

2. When the first class instance or any static member is referenced ,. net will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.

3. A class can have only one static constructor.

4. A non-parameter constructor 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 is no conflict.

5. Run only once at most.

6. Static constructors cannot be inherited.

7. If no static constructor is written and the class contains static members with an initial value, the compiler automatically generates the default static constructor.

 

Static constructor fun talk!

The static constructor of a class is also called the Type constructor. It is controlled by the CLR at the time of its call:

CLR selects one of the following time to call the static constructor:
1. Before the first instance of the type is created, or before the non-inherited field of the type or the first access by the Member. The "before" here indicates the meaning of the front and back links. The time here is accurate!
2. The time before a non-inherited static field or a member's first visit may vary!

Since the call time is unknown, we 'd better not compile the execution sequence that depends on the specific static constructor.CodeIn this way, unpredictable consequences are easily generated!

Let's take a closer look at some interesting behavior of the static constructor by taking a look at three demos:

Demo1:

Static void main (string [] ARGs)
{
Console. writeline (B. strtext );
}
Public Class
{
Public static string strtext;
Static ()
{
Strtext = "aaaa ";
}
}
Public Class B:
{
Static B ()
{
Strtext = "BBBB ";
}
}

You can guess what the result is. Some people may think that the output is Bbbb. To access B. strtext, you need to call the static constructor static B () of Class B (). In fact, the output result is AAAA. Because strtext is a static field of Class A, and Class B only inherits this field, the static constructor static A () of Class A will be called here (), the output result is AAAA. There is nothing 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 = new B ();
A A = new ();

Console. writeline (B. strtext );

}
Public Class
{
Public static string strtext;
Static ()
{
Strtext = "aaaa ";
}
}
Public Class B:
{
Static B ()
{
Strtext = "BBBB ";
}
}
You can guess what the output result is. Some people may think that AAAA will be output because static B () is called before new B (), and static A needs to be called before new, the result is AAAA, but it is not true. The correct result is BBBB for the following reasons:

Before executing new B ();, the static constructor of Class B will call, that is, it will call:
Static B ()
{
Strtext = "BBBB ";
}

When strtext = "BBBB" is executed, you need to access the strtext field, and the strtext field of B is inherited from Class A. Therefore, you need to first call:
Static ()
{
Strtext = "aaaa ";
}
After this function is executed, the strtext value is AAAA.
Then the code returns to static B (). Then, the strtext = "BBBB" line in static B () is executed. Therefore, the value of strtext is Bbbb.
When a A = new A (); is executed, the static constructor of A is not called, because it has been called before, and the static function is used in the entire application.ProgramThe domain will be called only once in its lifecycle!

Please give me more advice!

The static constructor of a class is also called the Type constructor. It is controlled by the CLR at the time of its call:

CLR selects one of the following time to call the static constructor:
1. Before the first instance of the type is created, or before the non-inherited field of the type or the first access by the Member. The "before" here indicates the meaning of the front and back links. The time here is accurate!
2. The time before a non-inherited static field or a member's first visit may vary!

Since the call time is unknown, we 'd better not compile code that depends on the execution sequence of a specific static constructor. This will easily lead to unpredictable consequences!

Let's take a closer look at some interesting behavior of the static constructor by taking a look at three demos:

Demo1:

Static void main (string [] ARGs)
{
Console. writeline (B. strtext );
}
Public Class
{
Public static string strtext;
Static ()
{
Strtext = "aaaa ";
}
}
Public Class B:
{
Static B ()
{
Strtext = "BBBB ";
}
}

You can guess what the result is. Some people may think that the output is Bbbb. To access B. strtext, you need to call the static constructor static B () of Class B (). In fact, the output result is AAAA. Because strtext is a static field of Class A, and Class B only inherits this field, the static constructor static A () of Class A will be called here (), the output result is AAAA. There is nothing 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 = new B ();
A A = new ();

Console. writeline (B. strtext );

}
Public Class
{
Public static string strtext;
Static ()
{
Strtext = "aaaa ";
}
}
Public Class B:
{
Static B ()
{
Strtext = "BBBB ";
}
}
You can guess what the output result is. Some people may think that AAAA will be output because static B () is called before new B (), and static A needs to be called before new, the result is AAAA, but it is not true. The correct result is BBBB for the following reasons:

Before executing new B ();, the static constructor of Class B will call, that is, it will call:
Static B ()
{
Strtext = "BBBB ";
}

When strtext = "BBBB" is executed, you need to access the strtext field, and the strtext field of B is inherited from Class A. Therefore, you need to first call:
Static ()
{
Strtext = "aaaa ";
}
After this function is executed, the strtext value is AAAA.
Then the code returns to static B (). Then, the strtext = "BBBB" line in static B () is executed. Therefore, the value of strtext is Bbbb.
When a A = new A (); is executed, the static constructor of A is not called because it has already been called, static functions are called only once throughout the lifecycle of the application domain!

Please give me more advice!

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.