Static member initialization and precedence of constructors

Source: Internet
Author: User

Recently the company has a project database that needs to be migrated from Oracle to SQL Server, to facilitate my introduction of a lightweight ORM framework Petapoco, and a simple encapsulation based on the relevant business. Before the company's project structure is very humble, the function is difficult to expand, very little use of the framework, the code with a lot of static methods, it may feel that the direct use of the name of the method is very convenient, and not to consider the burden of the server, code layering confusion, let me not believe this is a listed company's strength. It could be just. NET this piece is relatively weak. As a fresh graduate, after all, it is less than one months to come here.

Anyway, because the project is a static method, and I joined the encapsulated code, there is a confusion, I let the operation of the database classes inherit a parent class, in order to be able to public dbcontext, but my DbContext object is not static, so the static method can not access, I can only change to a static member, but DbContext declares that I did not initialize it, but instead put it in the constructor of the parent class, and the constructor is also non-static. At the time of the revision, and did not think too much, tonight suddenly remembered to write this may have "object reference is a instance of an object" error, because the sense of static resources to precede the constructor, here really just feel, not seriously studied. Then I found a piece of paper in the garden and answered my doubts. Summary of the article: when compiling, the compiler will parse the required static fields beforehand, if the static fields are in a static constructor, then the initialization of the field is ignored, otherwise the initialization of the static field is performed first. The order in which static members of a class is initialized depends on the order of reference in the main function, the advanced row initialization referred to first, but if the initialization of a static member of a class relies on a static member of another class, the static member of the dependent class is initialized first. A static field of a class with a static constructor is initialized only when it is referenced.

Original address: http://www.cnblogs.com/cpcpc/archive/2010/04/16/2123135.html

Static member initialization and precedence of 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.