[Mono-list] Static constructor issue

Abe Gillespie abe.gillespie at gmail.com
Thu Sep 7 14:28:18 EDT 2006


static Child.ctor is called first and then static Parent.ctor is
called ... that's just the way the order of operations go.

The metaphor I was given way back in my student days is this:
Think of building a watch, you first start with the internals (gears
and such) and this can be seen as the Child.  Then you put on the
outer parts (housing, glass, etc.) where this is seen as the Parent.

The reason why it's different for instance ctors is because you call
the Parent ctor specifically before the code inside the Child ctor
runs.

Good luck.
-Abe

FYI - I didn't mean to email you directly and put the mono list back in.

On 9/7/06, Andreas Färber <andreas.faerber at web.de> wrote:
> Hi,
>
> Am 07.09.2006 um 20:09 schrieb Abe Gillespie:
>
> > I'm not sure if this is your situation (you left out the necessary
> > details) ... but you need to call your base class's constructor
> > directly like so:
> >
> > class Parent
> > {
> >  public Parent(int foo)
> >  {
> >    // Do something with foo ...
> >  }
> > }
> >
> > class Child : Parent
> > {
> >  public Child(int foo) : base(foo)
> >  {
> >    // Do something specific with Child.
> >  }
> > }
> >
> > Good luck!
> > -Abe
>
> No, I wasn't talking of instance constructors. An example:
>
> class Parent {
>         static Parent() {
>                 // important stuff
>         }
>         public Parent(int foo) {
>         }
> }
>
> class Child : Parent {
>         static Child() {
>                 // stuff that relies on "important stuff"
>         }
>         public Child(int foo) : base(foo) {
>         }
> }
>
> Code then does new Child(42);.
>
> I would assume that before Child.cctor is called, Parent.cctor would
> need to be called by the runtime - which apparently is not happening.
> So I'm wondering whether this is a Mono issue or whether it is
> intended that way.
>
> Andreas
>


More information about the Mono-list mailing list