Static constructor for static members

Source: Internet
Author: User

Static constructor
======================================
First, let's look at a piece of code:

Namespace Learning. Static
{
Class StaticTesting
{
Static void Main (string [] args)
{
Test t1 = new test ();
Test t2 = new test ();
Console. WriteLine (test. count );
}
}
Class test
{
Public static int count;
Static test ()
{
Count ++;
}
Public test ()
{
Count ++;
}
}
}

Q: What is the output result of the Console? Why?
Some students say "4", because the constructor will be executed every time new times. This test class has two constructor functions, and it must be 4 after new times.
Some may say 3.
A: 3
So why is it 3 instead of 4? We need to explore the static constructor in the test class.

First, let's slightly modify the code of test class, as shown below:
Class test
{
Public static int count;
Static Test ()
{
Count ++;
Console. writeline ("static test ()");
}
Public test ()
{
Count ++;
Console. writeline ("test ()");
}
}

Run the code again. The result is as follows:
Static Test ()
Test ()
Test ()
3
Obviously, the static constructor of test class is executed only once, although we have two New instances in Main.

On MSDN:
A static constructor is used to initialize any static data, or to perform a participant action that needs must med once only. It is called automatically

Before the first instance is created or any static members are referenced.

1) A static constructor does not take access modifiers or have parameters.
Parameters and access modifiers are not allowed.

2) A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
This statement tells us that the static constructor is called before the first instance is created or the static member is referenced. for the above example, if we follow the F11 single step, tracking count will find that its value is 1 before new t1 calls Public test.
If we modify the Main () function as follows:
Static void Main (string [] args)
{
Console. writeline (test. Count );
Console. Read ();
}
At this time, the output Count value is 1 (because there is no new, non-static constructor is not called). Although we didn't come out of the new instance, we call test, a static member of test. count also executes the test static constructor.

3) a static constructor cannot be called directly.

4) The user has no control on when the static constructor is executed in the program.

5) a typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

6) Static constructors are also useful when creating Wrapper Classes for unmanaged code, when the constructor can call the loadlibrary method.

7) If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

As described above, we should understand it.

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.