C # static Constructors and constructors

Source: Internet
Author: User
Tags modifiers

The concept of constructors, when we first began to learn programming language, was taught over and over again by the teacher. Pro, now do you remember the appropriate scenario for static constructors? If not, then let's review it together.

A static constructor is formed after the static keyword was added before the constructor method, and there are no modifiers (public,private), no parameters.

What are the characteristics of static constructors:

    1. Static constructors do not have modifier modifiers (public,private), because static constructors are not called by our programmers, but are called by the. NET Framework at the right time.
    2. The static constructor has no arguments, because the framework cannot know what parameters we need to add to the function, so the argument cannot be used.
    3. The static constructor must be preceded by the static keyword. If you don't add this keyword, that's the normal constructor.
    4. An instance variable cannot be instantiated in a static constructor. (variables can be divided into class-level and instance-level variables, where the class-level has static keyword adornments).
    5. The timing of the invocation of a static function is called when the class is instantiated or when a static member is called, and the static constructor is called by the. NET Framework to initialize the static member variable.
    6. There can be only one static constructor in a class.
    7. A parameterless static constructor and a parameterless constructor can coexist. Because they belong to the class level, one belongs to the instance level and does not conflict.
    8. The static constructor is only executed once. and is called in the timing of the call in feature 5.
    9. As if the constructor is not written in the class, then the framework will generate a constructor for us, so if we define a static variable in the class, but do not define a static constructor, then the framework will also help us to generate a static constructor for the framework to invoke itself.

The above features are theoretical, let's do a wrong problem and help us learn about static constructors together.

1PublicClassC2{3PublicStaticStringBB;4PublicStaticC ()5{6 BB ="Cc";7}8PublicStatic C (Stringmm9{Ten BB =mm11}12StaticC ()13{BB ="Right";15}16 public C () 17  {18 BB =  "wrong" ;19 }20 public C (string mm) 21  {22 BB = Mm; }24}        

VS Compile prompt error message:

Now let's do an interesting thing to verify the call timing of the static constructor:

1ClassProgram2{3Staticvoid Main (String[] args)4{56Console.WriteLine (A.strtext);7Console.WriteLine (B.strtext);8Console.read ();9}10}11PublicClassA12{13PublicStaticStringStrText;14PublicStringText;15StaticA ()16{StrText ="Aaa";18}19PublicA ()20{Text ="Aaaaaaaaaaaaaaaaaaaaaaaaaa";22}23}24PublicClassB:a25{26static B () 27  {28 strText =  "bbb< Span style= "color: #800000;" > "; 29 }30 public B () 31  {32 Text =  "bbbbbbbbbbbbbbbbb  "; }34}        

The results are: AAA

Let's analyze why this happens, when the a.strtext is displayed, because strtext is a static variable, the framework calls the static constructor of a, at which point the value of strtext is AAA. correct

When B.strtext is displayed, because B inherits from a, the static constructor of a is called first, but because the static constructor is called only once, the static constructor of a is not called, but because strtext belongs to Class A rather than B, the static constructor of B is not executed, so the output is AAA.

But if we change the output, the output will be very different.

1ClassProgram2{3Staticvoid Main (String[] args)4{5 b b =NewB ();6 A A =NewA ();78Console.WriteLine (A.strtext);9Console.WriteLine (B.strtext);1011Console.read ();12}13}14PublicClassA15{16PublicStaticStringStrText;17PublicStringText;18StaticA ()19{StrText ="Aaa";21st}22PublicA ()23{The Text ="Aaaaaaaaaaaaaaaaaaaaaaaaaa";25}26}27PublicClassB:a28{29static B () 30  {31 strText =  "bbb< Span style= "color: #800000;" > "; 32 }33 public B () 34  {35 Text =  "bbbbbbbbbbbbbbbbb  "; }37}        

Notice that I instantiate the class at the beginning, so the output is BBB at this point.

Why is there such a situation, in fact, still need to start from the static constructor call time.

First we instantiate B, and then we call the static constructor of B, but since strtext is a static variable, we first call A's static constructor to assign the strtext to AAA, and then call B's static constructor to assign the strtext to BBB. So at this point the value of strtext should be BBB, so the output is BBB.

C # static Constructors and constructors

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.