Profic and disadvantages on static construction functions

xiaoxiao2021-03-05  26

The static constructor will perform in both cases: 1. Developers add a static, no parameter method with the same name in the C #, to explicitly add a static constructor. 2, the type has an initial value setting item for static fields, in which case the compiler adds a static constructor in the background.

The first case will result in deterioration in code performance, and Microsoft recommends initializing it in the position of the static field. Refine the generated code Discovery: 1 Insert in order to perform an inspection performed in the exact time to run the initial value setting item, increase the overhead within the circulation. 1 to 2 brings performance to performance, the benefit of exchange is that static variables will be initialized in precisely expected time.

There are very few classes that truly need a static constructor to execute in a precise time, so in most cases, it is very meaningful to avoid explicit static constructive functions.

转载请注明原文地址:https://www.9cbs.com/read-34481.html

New Post(0)