[Mono-devel-list] Static fields initialization
mono at evain.net
mono at evain.net
Thu Nov 4 14:48:23 EST 2004
Hello,
The Mono runtime and the MS CLR does not behave in the same way for static
fields initialization. Here is a simple example :
.assembly extern mscorlib {}
.assembly test {}
.module test.exe
.class public EntryPoint extends [mscorlib]System.Object {
.field public static int32 m_test = int32(2)
.method public static void Main() cil managed {
.entrypoint
ldsfld int32 EntryPoint::m_test
box [mscorlib]System.Int32
call void [mscorlib]System.Console::WriteLine(object)
ret
}
}
Using Mono runtime, the output is 2, but with MS, it is 0.
If I found that Mono's behaviour is much more logical, here is a quote from
the ECMA CLI Standard (line 14, page 85):
"The <fieldInit> information is stored in metadata and this information can
be queried from metadata. But the CLI does not use this information to
automatically initialize the corresponding fields."
And here is a quote from Inside MS.NET IL Assembler :
"What will the value of the field be when the class is loaded? Correct
answer: 0. Why? Because default values specified in the Constant table are
not used by the loader to initialize the items to which they are assigned.
If we want to initialize a field to its default value, we must explicitly
call the respective Reflection method to retrieve the value from metadata
and then store this value in the field. This doesn't sound too nice, but, on
the other hand, we should not forget that these are default values rather
than initial values, so formally the loader might be right."
I've already told that I do prefer Mono's behaviour here, but I would like
to have some guru's opinions about this.
Thanks,
Jean-Baptiste Evain
More information about the Mono-devel-list
mailing list