I have a question concerning type constructors within a Value type. This question was inspired by something that Jeffrey Richter wrote in CLR via C# 3rd ed, he says (on page 195 - chapter 8) that you should never actually define a type constructor within a value type as there are times when the CLR will not call it.
So, for example (well...Jeffrey Richters example actually), I can't work out, even by looking at the IL, why the type constructor is not being called in the following code:
internal struct SomeValType
{
static SomeValType()
{
Console.WriteLine("This never gets displayed");
}
public Int32 _x;
}
public sealed class Program
{
static void Main(string[] args)
{
SomeValType[] a = new SomeValType[10];
a[0]._x = 123;
Console.WriteLine(a[0]._x); //Displays 123
}
}
So, applying the following rules for type constructors I just can't see why the value type constructor above is not called at all.
- I can define a static value type constructor to set the initial state of the type.
- A type can have no more than one constructor - there is no default one.
- Type constructors are implicitly private
- The JIT compiler checks whether the type's type constructor has already been executed in this AppDomain. If not it emits the call into native code, else it doesn't as it knows the type is already 'initialized'.
So...I just can't work out why I can't see this type array being constructed.
My best guess would be that it could be:
- The way that the CLR constructs a type array. I would have thought that the static constructor would be called when the first item was created
- The code in the constructor is not initializing any static fields so it is ignored. I have experimented with initializing private static fields within the constructor but the field remains the default 0 value - therefore the constructor is not called.
- Or...the compiler is somehow optimizing away the constructor call due to the public Int32 being set - but that is a fuzzy guess at best!!
Best practices etc asside, I am just super intrigued by it as I want to be able to see for myself why it doesn't get called.
EDIT: I added an answer to my own question below, just a quote of what Jeffrey Richter says about it.
If anyone has any ideas then that would be brilliant.
Many thanks,
James
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…