cs--Static Constructors

Source: Internet
Author: User

Define the following test class public class Test{public static int msA = 10;static Test () {System.Console.WriteLine (string. Format ("msa0:{0}", MSA); MSA = MSA + MSA; System.Console.WriteLine (String. Format ("msa1:{0}", MsA);}  public Test () {MsA = 30; System.Console.WriteLine (String. Format ("msa2:{0}", MsA);}  public static int Geta () {System.Console.WriteLine (string. Format ("msa5:{0}", MSA); return MSA;}}  public class Testchild:test{public static int msA = 40;} Execute the following code in the main function System.Console.WriteLine ("Test begin"); System.Console.WriteLine (String. Format ("msa6:{0}", Testchild.msa)); System.Console.WriteLine (String. Format ("msa3:{0}", Test.geta ())); System.Console.WriteLine (String. Format ("msa3:{0}", Test.msa)); Test obj1 = new test (); System.Console.WriteLine (String. Format ("msa4:{0}", Test.msa)); System.Console.WriteLine (String. Format ("msa6:{0}", Testchild.msa)); the output is as follows Test BEGINMSA6:40MSA0:10MSA1:20MSA5:20MSA3:20MSA2:30MSA4:30MSA6 : 40 Conclusion: (1) A static constructor is called before the first instantiation or referencing any static member (variable or method) and cannot be called directly. (2) The static constructor is executed only once. (3) Static constructors do not allow access to repairDecoration and parameters, or compile an error. (4) A static constructor is not inherited, that is, the child class will not be called until the first instantiation or reference to any static member (variable or method) is a static constructor of the parent class. (5) Static constructors are allowed to not be defined and can coexist with parameterless constructors. (6) Static constructors are thread-safe. This article references and references the following picture http://blog.csdn.net/xxdddail/article/details/38058121

cs--Static constructor

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.