Class Constructors

Class Constructors

Class constructors, or type initializers, are the methods specific to a type as a whole that run after the type is loaded and before any of the type’s members are accessed. You’ve already encountered class constructors in the preceding chapter, which discussed approaches to static field initialization. That is exactly what class constructors are used for: static field initialization.

Class constructors are static, have specialname and rtspecialname flags, have neither parameters nor return value—that is, the return type is void—and have the name .cctor, which in ILAsm is a keyword rather than a name. Only one class constructor per type is permitted, and it cannot use the vararg calling convention.

Normally, class constructors are never called from the IL code. If a type has a class constructor, this constructor is executed automatically after the type is loaded. However, a class constructor, like any other static method, can be called explicitly. As a result of such a call, the global fields of the type are reset to their initial values. Calling .cctor explicitly does not lead to type reloading.



Inside Microsoft. NET IL Assembler
Inside Microsoft .NET IL Assembler
ISBN: 0735615470
EAN: 2147483647
Year: 2005
Pages: 147
Authors: SERGE LIDIN

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net