tags:

views:

522

answers:

5

In C#, when you do

Class(Type param1, Type param2):base(param1)

is the constructor of the class executed first, and then the superclass constructor is called or does it call the base constructor first?

+7  A: 

It will call the base constructor first. Also keep in mind that if you don't put the :base(param1) after your constructor, the base's empty constructor will be called.

Yuriy Faktorovich
+2  A: 

The constructor of the baseclass is called first.

tanascius
A: 

Your question is a bit unclear but I'm assuming you meant to ask the following

When to I call the base constructor for my XNA object vs. using the impilict default constructor

The answer to this is highly dependent on both your scenario and the underlying object. Could you clarify a bit wit the following

  • What is the scenario
  • What is the type of the base object of TerrainCollision?

My best answer though is that in the case where you have parameters that line up with the parameters of the base class`s constructor, you should almost certainly be calling it.

JaredPar
+1  A: 

[Edit: in the time it took me to answer, the question had totally changed].

The answer is that it calls the base first.

[Original answer to the old question below]

Are you asking when you would do the "base" bit of the constructor call?

If so, you would "chain" a call to the constructor base if the class is derived from another class which has this constructor:

  public class CollisionBase
    {
        public CollisionBase(Body body, GameObject entity)
        {

        }
    }

    public class TerrainCollision : CollisionBase
    {
        public TerrainCollision(Body body, GameObject entity)
            : base(body, entity)
        {

        }
    }

In this example, TerrainCollision derives from CollisionBase. By chaining the constructors in this way, it ensures the specified constructor is called on the base class with the supplied parameters, rather than the default constructor (if there is one on the base)

Rob Levine
+5  A: 

The order is:

  • Member variables are initialized to default values for all classes in the hierarchy

Then starting with the most derived class:

  • Variable initializers are executed for the most-derived type
  • Constructor chaining works out which base class constructor is going to be called
  • The base class is initialized (recurse all of this :)
  • The constructor bodies in the chain in this class are executed (note that there can be more than one if they're chained with Foo() : this(...) etc

Note that in Java, the base class is initialized before variable initializers are run. If you ever port any code, this is an important difference to know about :)

I have a page with more details if you're interested.

Jon Skeet
It seems like half of my contributions here are pointing to his answers; but Eric Lippert wrote a good pair of posts on this topic as well:http://blogs.msdn.com/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspxhttp://blogs.msdn.com/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx
Matt Enright
Including the effects of collection initializers, a bit more in depth than the question perhaps, but a good read.
Matt Enright