If the subclass is not instantiated, it will not call its own static constructor.

Source: Internet
Author: User
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace consoleapplication6 {class program {static void main (string [] ARGs (); stat2 st2 = new stat2 (); # endregion stat2.dosomething (); console. readkey () ;}} class STAT1 {public static STAT1 instance; // static STAT1 () // {// console. writeline ("STAT1 static structor"); //} public static void dosomething () {instance. do ();} static STAT1 () {console. writeline ("STAT1 static costructor"); instance = new STAT1 ();} Public Virtual Void Do () {console. writeline ("STAT1 do") ;}} class stat2: STAT1 {static stat2 () {console. writeline ("stat2 static costructor"); instance = new stat2 ();} public override Void Do () {console. writeline ("stat2 do ");}}}

If the base class STAT1 is not instantiated, its static method is called first.
However, if the stat2 subclass is not instantiated, it neither calls the constructor of the base class STAT1 nor its own static constructor.

Isn't that strange?

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.