31

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?

Community
  • 1
  • 1
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
  • 2
    Bad practice to have member variables become public. You may want to pick up a good book on beginning C# objects. – JonH Jan 07 '10 at 15:36
  • 3
    Duplicate of http://stackoverflow.com/questions/1882692/c-constructor-execution-order, with a terrific accepted answer and comments linking Eric Lippert blog entries no less. – Jeff Sternal Jan 07 '10 at 15:41

6 Answers6

48

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/

Paolo
  • 22,188
  • 6
  • 42
  • 49
  • Good to know the initializers are in opposite order. But why is it designed in this way, to have 2 different orders? – RayLuo Apr 03 '17 at 09:06
  • 1
    @RayLuo Explained in a comment of the duplicate answer. It links to a page whose "Part 2" is [here](https://blogs.msdn.microsoft.com/ericlippert/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two/) and says: "“if the constructors and initializers run in their actual order then an initialized readonly field of reference type is guaranteed to be non null in any possible call. That guarantee cannot be met if the initializers run in the expected order.” – Quantic May 16 '17 at 17:37
4

First base class constructor is called followed by the derived class constructor. The result is 2. You should explicitly state the accessibility of that class variable. Is it protected, private or public?

I see you changed it to public now, so it will be 2.

This link will further help you understand constructors, how they are used, when they are called, and order of constructor call when you use inheritance:

http://www.yoda.arachsys.com/csharp/constructors.html

Also you may want to actually try this out yourself, you will learn more by practicing and writing code then just reading it.

Try to declare Bar and output its value. Use some properties:

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

        public int ABC
        {
            get { return abc; }
            set { abc = value; }
        }

    }

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


    class Program
    {
        static void Main(string[] args)
        {
            Bar b = new Bar();
            Console.WriteLine(b.ABC);
            Console.ReadLine();

        }
    }

A simple printout would yield the result you are looking for. Here is the output:

alt text

Don't you just love my namespace :-). By the way you could also use automatic properties so that the property is simply public int ABC {get;set;}.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
JonH
  • 32,732
  • 12
  • 87
  • 145
2

Assuming you make abc protected so that this compiles, it will be 2; however, base() is called first.

For stuff like this, write a simple test application and setup some breakpoints to find the answer.

RBT
  • 24,161
  • 21
  • 159
  • 240
bradjive
  • 1,482
  • 2
  • 13
  • 17
1

The variable abc will be set to be 3 and then changed to be 2 (the base constructor is called first).

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
1

The base constructor will be called first, but this code does not compile. Private fields are not accesable from sub-classes. At the very least a field must be protected to be used in a sub-class.

But even knowing this, the behaviour you are attempting is confusing because it is surprising. Just the fact you had to ask which order things go in implies that it will get messed up when the order is forgotten.

Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
1

The base constuctor is called first, and you would have a value of 2 for abc

Aaron M
  • 2,523
  • 1
  • 23
  • 38