C # static constructors and static members of generic classes [classic]

Source: Internet
Author: User

C # static constructors and static members of generic classes [classic]

 

Is a static constructor always called at most once? Will static constructors be called? Take the following example:

Namespace
Leleapplication1
{
Class program: A <float>
{

Static Program ()
{
Console. writeline ("Class
Program static construct invoked .");
}

Static
Void main (string [] ARGs)
{

Console. writeline (B. somevalue ++ );

Console. writeline (C. somevalue ++ );

Console. writeline (D. somevalue ++ );

Console. writeline (E. somevalue ++ );

Console. writeline (E. somevalue2 ++ );

New F ();

New F ();

Console. readkey ();

}
}


Class A <t>
{
Public static int somevalue = 0;


Public ()
{
Console. writeline ("Class
A <{0}> dynamic construct invoked. ", typeof (T). Name );
}


Static ()
{
Console. writeline ("Class
A <{0}> static construct invoked. ", typeof (T). Name );
}

}

Class B: A <string>
{
Static B ()

{
Console. writeline ("Class B static construct
Invoked .");
}
}

Class C: A <int>

{
Static C ()
{

Console. writeline ("Class C static construct invoked .");
}

}

Class D: A <int>
{
Static D ()

{
Console. writeline ("Class D Static construct
Invoked .");
}
}

Class E: A <float>

{
Public static int somevalue2 = 0;
Static E ()

{
Console. writeline ("Class E static construct
Invoked .");
}
}

Class F: A <bool>

{
Public F ()
{

Console. writeline ("Class F dynamic construct invoked .");
}


Static F ()
{
Console. writeline ("Class F
Static construct invoked .");
}
}

}

Output
As follows:
Class programe static construct invoked.
Class A <string>
Static construct invoked.
0
Class A <int32> static construct
Invoked.
0
1
Class A <single> static construct invoked.
0
Class
E static construct invoked.
0
Class F static construct invoked.
Class
A <Boolean> static construct invoked.
Class A <Boolean>
Dynamic construct invoked.
Class F dynamic construct invoked.
Class
A <Boolean> dynamic construct invoked.
Class F dynamic
Construct invoked.

The output result is analyzed as follows:

// The program is actually called by the framework.
But does not instantiate program.
Class program static construct invoked.
//
In fact, static members do not support inheritance. The C # compiler translates B. somevalue into a <string>. somevalue at the underlying layer.
// Therefore
Load not B, but a <string>
Class A <string> static construct
Invoked.
0
// Similarly, C. somevalue is translated as a <int>. somevalue
Class
A <int32> static construct invoked.
// What's strange? Isn't the inherited somevalue added,
Why is this still 0?
0
// You Can See That somevalue is actually added, but a <string> and a <int> are
Different Types
// The somevalue is also independent of each other.
1
// E. somevalue is translated
A <float>. somevalue
Class A <single> static construct
Invoked.
0
// Here E. somevalue2 is far from the original E. somevalue2. Of course, E's static constructor will be executed.
//
Now we find that the static constructor B/C/D has not been executed. The reason is that we have just mentioned that, in fact
// The reference of B/C/D above is translated into a closed class at the underlying layer,
Therefore, the load is not B/C/D,
// Corresponding closed class
Class E static construct invoked.
0
//
Now let's take a look at the execution sequence of static constructor and constructor.
// Because f is new, F is first loaded and then the static structure of F is executed.
Class F
Static construct invoked.
// After loading f, it is found that F is inherited from a <bool> and then loaded.
A <bool>, so the static structure of a <bool> is executed.
Class A <Boolean> static
Construct invoked.
// First execute the construction method of the base class
Class A <Boolean> dynamic
Construct invoked.
// F's constructor
Class F dynamic construct
Invoked.
// Because a <bool> and F are loaded and the static structure has been executed, the constructor is called from low to high in order.
Class
A <Boolean> dynamic construct invoked.
Class F dynamic
Construct invoked.

Qq 1, 1163551688
Summary:
1. Various closed classes of generic classes belong to different classes and each has its own static space.
2.
Static members do not actually support inheritance. C # The Compiler determines the actual reference as a static member definer at the underlying layer.
3. The static constructor may not be executed because some classes are not executed.
Load
4. Static constructor is always called at most once, but static constructor of generic classes must be considered from another perspective.
5. The execution sequence of class constructor is bottom-up.
6.
The execution sequence of the class's static constructor is uncertain, because in the programming process, the class usage sequence is uncertain, and the static constructor is always executed in the place where the class is used for the first time.

 

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.