C# base() constructor order

Xenoprimate picture Xenoprimate · Jan 7, 2010 · Viewed 35.4k times · Source

Possible Duplicate:
C# constructor execution order

class Foo
{
    public int abc;
    Foo()
    {
       abc = 3;
    }

}

class Bar : Foo
{
    Bar() : base()
    {
       abc = 2;
    }
}

In the example above, when an object of Bar is created, what will be the value of BarObject.abc? Is the base constructor called first, or is Bar() run, /then/ the base() constructor?

Answer

Paolo picture Paolo · Jan 7, 2010

It'll be 2. Constructors run in order from base class first to inherited class last.

Note that initialisers (both static and instance variables) run in the opposite direction.

The full sequence is here: http://www.csharp411.com/c-object-initialization/