The time when static constructors and static member variables are called.

Source: Internet
Author: User
Class Class1
{
[Stathread]
Static   Void Main ( String [] ARGs)
{
System. Console. writeline (anobject =   Null );
System. Console. writeline ( " ProgramStart " );
}
Static Class1 ()
{
System. Console. writeline ( " Static constructor call " );
}
Static   Readonly   Object Anobject =   New   Object ();
}

One-step debugging concluded that before static constructor initialization, static constructor calls before static methods, and when is static member initialization called? Class Class1
{
[Stathread]
Static   Void Main ( String [] ARGs)
{
System. Console. writeline (anobject =   Null );
System. Console. writeline ( " Program start " );
Class2 C2 =   New Class2 ();
C2.do ();
Class2.staticdo ();

}
Static Class1 ()
{
System. Console. writeline ( " Static constructor call " );
}
Static   Readonly   Object Anobject =   New   Object ();
}

Public class class2
{< br> static class2 ()
{< br> system. console. writeline ( " class2 static constructor " );
}< br> Public class2 ()
{

}
Public   Void Do ()
{
System. Console. writeline ( " Class2 instance. Do () " );
}
Public   Static   Object Something =   New   Object ();

Public Static VoidStaticdo ()
{
System. Console. writeline ("Class2 static method do");
}
}

Trace debugging again. The sequence is as follows: when a class is first mentioned (static object/method is instantiated or called), the class starts to execute static member initialization. If a static constructor exists after initialization, the class calls the static constructor, then perform the corresponding operation.
In what order do multiple static members initialize? Public   Static   Object Something3 =   New   Object ();
Public   Static   Object Something1 =   New   Object ();
Public   Static   Object Something2 =   New   Object ();

Sequence 3-1-2, that is, the compilation Sequence
I don't know if this order will change because of the call. Public   Class Class2
{
Public   Static   Object Something3 = Something1;
Public   Static   Object Something1 =   New   Object ();
Public   Static   Object Something2 =   New   Object ();
}

class class1
{< br> [stathread]
static void main ( string [] ARGs)
{< br> system. console. writeline (class2.something3 = null );
}< BR >}

the result something3 is null, that is, it is not initialized because it is called first.

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.