I saw a question two days ago ~~ Class Program
{
Static Void Main ( String [] ARGs)
{
Caculator ca = New Caculator ();
Caculator CA1 = New Caculator ();
Console. writeline ( " Current object count is: " + Caculator. instancecount. tostring ());
Console. Readline ();
}
}
Public Class Caculator
{
Public Static Int Instancecount;
Static Caculator ()
{
Instancecount++;
}
Public Caculator ()
{
Instancecount++;
}
}
What is the output result ~~~
It should be 3, why ~~~? To understand this problem, you must first know when static functions will be called ~~, In. net. the static constructor cannot be called directly. Therefore, the public constructor is called during the instantiation operation. this question is a little different '~
For example, add a public parameter before the static caculator () method. As mentioned earlier, the static constructor cannot be called directly. Therefore, the public modifier is invalid, compilation fails. at the same time, this method cannot contain parameters (or because it cannot be manually called), otherwise it is also invalid.
So what is the use of this static constructor ~~ For example, you can use it to initialize the log class ~~ ApplicationProgramYou can use it to write logs.
It can also be used as a packaging class for unmanaged resources. Here you can call the loadlibrary method to select the version to be loaded.