Some Understandings of static constructor in C #

Source: Internet
Author: User

First, let's look at a conclusion table.

If (with class explicit static constructor)
{
If (with access to static members)
{
After entering the main function, call the static constructor before accessing the static member for the first time, and no static constructor will be called in the future. The same static constructor can only be called once at most.
}
Else
{
Do not call this type of static Constructor
}
}
Else
{
If (with access to static members)
{
In the execution of the main functionCodePreviously, the static constructor was called first, and no static constructor will be called later. The same static constructor can only be called once at most.
}
Else
{
Do not call this type of static Constructor
}
}

1. Let's verify the conclusion above.

Public class class_1 {public static int I; static class_1 () // explicitly defines the static constructor {console. writeline ("class_1: Display defined static constructor"); I = 1 ;}} public class class_2 {public static int I = Init (2 ); // no explicit static constructor public static int Init (Int J) {console. writeline ("class_2: no display defines static constructor"); Return J ;}} public class mainclass {static void main () {// analyze the results of the following situations }}
1 . The display defines static constructor and has access to static members.

Static VoidMain ()
{
///1. Display and define static constructor 2. Access static member I in class_1
Console. writeline ("Start in main:");
Console. writeline (class_1. I );
}

The result is as follows:

Start in main:
Class_1: displays and defines static constructors.
12. The static constructor is defined and no static members are accessed.
Static VoidMain ()
{
///1. Display definition static constructor 2. No access to static member I in class_1
Console. writeline ("Start in main:");
}

The result is as follows:
Start in main: 3 . No static constructor defined and accessed static members are displayed.
Static Void Main ()
{
/// 1. No definition of static constructor is displayed. 2. Access to static member I in class_2
Console. writeline ( " Start in main: " );
Console. writeline (class_2. I );
}
The result is as follows:
Class_2: no display defined static Constructor
Start in main:
2 4 . No definition static constructor is displayed and no static members are accessed.
Static Void Main ()
{
/// 1. No definition of static constructor is displayed. 2. No access to static members in class_2
Console. writeline ( " Start in main: " );
}
The result is as follows:
Start in main:

References:

1. Questions about type initializer and beforefieldinit to see if you can give the correct explanation (http://www.cnblogs.com/artech/archive/2008/11/01/1324280.html)

2. benefit from static content through seven key programming techniques (http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/us0501StaticsinNET.mspx? MFR = true)

3. Timing of execution of static Constructor (http://www.cnblogs.com/happyhippy/archive/2007/04/04/710936.html)

4. Taste details, deep into the. NET Type constructor (http://www.cnblogs.com/anytao/archive/2008/11/02/must_net_23.html)

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.