I guess so!
EDIT: The motivation for this question is: It seems to me that due to the semantics of static constructors they could never be 开发者_如何学Gosafely inlined.
The jitter's inlining strategy is an implementation detail -- subject to change at any time -- so there are pretty much no guarantees about what can or can't be inlined.
Having said that, it's difficult to see how a static constructor could ever be safely inlined, bearing in mind the guarantees provided by the C# and CLI specs regarding static constructors and type initialisation.
From the Microsoft C# specification (section 10.12):
The static constructor for a closed class type executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
- An instance of the class type is created.
- Any of the static members of the class type are referenced.
And from the ECMA CLI specification (section 8.9.5):
[A class can] optionally specify a method (called
.cctor
) to be called to initialize the type.The semantics of when and what triggers execution of such type initialization methods, is as follows:
- A type can have a type-initializer method, or not.
A type can be specified as having a relaxed semantic for its type-initializer method (for convenience below, we call this relaxed semantic BeforeFieldInit).
If marked BeforeFieldInit then the type's initializer method is executed at, or sometime before, first access to any static field defined for that type.
If not marked BeforeFieldInit then that type's initializer method is executed at (i.e., is triggered by):
- first access to any static field of that type, or
- first invocation of any static method of that type, or
- first invocation of any instance or virtual method of that type if it is a value type or
- first invocation of any constructor for that type.
(Note that C# classes with a static constructor will not have beforefieldinit
semantics. C# classes without a static constructor will have beforefieldinit
semantics.)
The only guarantees you have are the following :
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- The user has no control on when the static constructor is executed in the program.
精彩评论