RE: type initializers

From: Rick Byers [MSFT] (rbyers_at_online.microsoft.com)
Date: 08/17/04


Date: Tue, 17 Aug 2004 17:07:27 GMT

Hi George,
I'm no expert on the precise language of the CLI standard, but I can see
where there could be some subtle differences between the two descriptions.
(For the reference of others looking at this, it is in partition I section
8.9.5 - bottom of pg. 70 in the annotated standard book).

For example, it is actually possible to create an instance of an object
without calling it's constructor.
System.Runtime.Serialization.FormatterServices.GetUninitializedObject. does
this, which is used during deserialization (eg. BinaryFormatter, not
XmlSerializer).

In general, the standard authors probably prefer being explicit about
intended triggers, than to rely on interactions specified elsewhere that
could change in the future, or be wrong all together. So even if it was
only possible to access a field when a constructor had been called first,
it's probably better not to bake that assumption into the definition of
this behavior.

Rick

-- 
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> 
> My question is concerning the exact timing when a type initializer is 
> executed. 
> 
> In ECMA, I find the following: 
> 
> "If not marked BeforeFieldInit then that type’s initializer method is 
> executed at (i.e., is triggered 
> by): 
> - first access to any static or instance field of that type, or 
> - first invocation of any static, instance or virtual method of that 
type" 
> 
> Wouldn't be enough to say ONLY the following? 
> 
> The type initializer (of a BeforeFieldInit) type is triggered by: 
> - first access to any static field 
> - first invocation of any static method or instance constructor of that 
type 
> 
> My reasoning is: 
> 
> - if I access an instance field, then I should have already the instance, 
> right? And if I have the instance, it means I've already invoked an 
instance 
> constructor. 
> - the same for an instance method (or virtual method). 
> 
> Am I wrong? Is there something that I miss here? 
> 
> Many thanks. 
> George 
> 


Relevant Pages

  • Re: type initializers
    ... a constructor -- if you specify a local variable of a value type, ... > My question is concerning the exact timing when a type initializer is ... > - first access to any static field ... > - if I access an instance field, then I should have already the instance, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Any experience with "The Last One"?
    ... In this International Standard, the examples, the notes, the ... xconstructor ... xdestructor ...
    (comp.lang.c)
  • Re: Any experience with "The Last One"?
    ... In this International Standard, the examples, the notes, the ... xconstructor ... xdestructor ...
    (comp.programming)
  • Re: base/derived name unhiding but with ctors
    ... Well, one problem with that terminology is that, when reading those parts ... of the standard that refer to constructor "calls", ... including large parts of the standard. ... of the subject matter (which is the normal context sensitivity of words) ...
    (comp.lang.cpp)
  • Re: initializing static class variables
    ... >> I believe it was introduced in 2003 standard but I can't be sure since I ... >> default constructor is called. ... the definition of compiler generated default constructor. ... constructed with default initialization. ...
    (comp.lang.cpp)