Static constructor, which is rarely used in East China.
Static constructor is still interesting. It has the following characteristics:
1) The static constructor is executed before the constructor is executed.
2) When a static member of the call class is called, the static constructor is executed first.
3) The static constructor is executed only once. (No matter whether you create an object or call a static member of the class)
The following are classes that contain static constructors. Public Class Test
{
Private Static String _ Name;
Public Static StringName
{
Get{Return_ Name ;}
Set{_ Name=Value ;}
}
StaticTest ()
{
Console. writeline ("Static Constructor");
}
PublicTest (StringName)
{
_ Name=Name;
}
Public test ()
{< br> console. writeline ( " constructor " );
}
Public Static VoidMethod ()
{
Console. writeline ("Static Functions");
}
}
Test 1:
Class stater
{
Static void main ()
{
Test test = new test ();
Console. readkey ();
}
}
Result:
Test 2:
Class stater
{
Static void main ()
{
Testing. Name = "123 ";
Test. Method ();
Console. readkey ();
}
}
Result:
Test 3:
Class stater
{
Static void main ()
{
Test test = new test ();
Test Test2 = new test ("landpy ");
Test test3 = new test ();
Test. Method ();
Test. Name = "landpy ";
Console. readkey ();
}
}
Result: