Possible Duplicate:
Why doesn't the CLR always call value type constructors
Found next code in Richter's book (I have simplified it a little bit):
internal struct SomeValType
{
static SomeValType()
{
Console.WriteLine("This never gets displayed");
}
public Int32 X;
}
public sealed class Program
{
public static void Main()
{
SomeValType a = new SomeValType { X = 123 };
Console.WriteLine(a.X);
}
}
Output:
123
Can't understand why WriteLine
in static constructor nevel calls. Looked at ILDasm - constructor code and calling of method are present. If I add any static variable to SomeValType
and init it in constructor then WriteLine
calling correctly.
Can someone explain, please, behavior in such situation? Thanks.