views:

79

answers:

5

For C# in VS2005, will calling this() in an inherited class cause the execution of the base constructor?

EDIT: How can I avoid having to re-write the x and y assignments? Note, I do not want the MyObject(int num) constructor to execute the base() contructor.

public class MyObject : MyParentObject { 

int x; 
int y; 
int z; 

public MyObject() { 
    x = 5; 
    y = 10; 
} 

public MyObject(int num) : base(num) { 
    x = 5; 
    y = 10; 
    z = num; 
} 
+1  A: 

I believe this is the syntax you are looking for:

    class MainClass
{
    MainClass()
    {
        //do something
    }

}

class MyClass : MainClass
{
    MyClass()
        : base()
    {
        // do something else
    }

}

Calling base() will cause it to run the base constructor before the current constructor.

highphilosopher
Interesting. I answer first, then you answer the same thing, and all of the sudden my answer has one down vote. Gee, I wonder what's going on here.
Esteban Araya
Whether or not you call `base()`, it will still run before the derived class constructor, assuming the base class has a parameter-less constructor *and* you do not explicitly call a parameterized base constructor. In any event, the base class is going to be constructed before the derived class.
Anthony Pegram
This answer isn't wrong, but it's unnecessary. And @Esteban, I'm the one that down-voted you.
Matthew Flaschen
@Esteban, I didn't see your answer until after I clicked the submit button. There was literally 13 seconds between the two posts. If you're worried about being copied, I really can't type that fast.
highphilosopher
+5  A: 

base() will be called implicitly by the first constructor to run in the derived class:

public MyObject() {
    x = 5;
    y = 10;
}

public MyObject(int setZ) : this() {
    z = setZ;
}

is equivalent to:

public MyObject() : base() {
    x = 5;
    y = 10;
}

public MyObject(int setZ) : this() {
    z = setZ;
}
Matthew Flaschen
+1  A: 
class BaseClass
{
   BaseClass()
   {

   }
}

class MyClass : BaseClass
{
   MyClass(int? id) : base()
   {

   }

   MyClass() : this(null)
   {

   }
}

MyClass() : this(null) will call base via MyClass(int? id)

Or you could swap it around and make MyClass(int? id) : this() and MyClass() : base() either way, the base constructor will be called.

The paramater-less constructor (if there is one) is called if nothing is specified, or a compiler error will result (if you only have a base constructor with parameters)

PostMan
+2  A: 

The parameterless base constructor will be called implicitly unless you explicitly call a paramterized base constructor.

If you have

class Base { }
class Foo : Base { public Foo() { } }

It is no different from saying

class Foo : Base { public Foo() : base() { } }

So if you have a parameterized constructor for Foo, base() will be called no matter what you do with this() unless you also have a parameterized constructor for Base that you explicitly call.

class Base
{
    public Base() { }
    public Base(int bar) { }
}

class Foo : Base
{
    public Foo() : base() { }
    public Foo(int bar) : base(bar) { }
    // also legal: 
    // public Foo() : base(1) { }
    // public Foo(int bar) : base(1) { }
    // public Foo(int bar) : base() { }
    // public Foo(int bar) { } /* uses implicit call to base() */
    // public Foo() { } /* ditto */
}

Either way, the base class will get instantiated first either through the parameterless constructor (implicitly or explicitly) or through the parameterized constructor (explicitly).

Anthony Pegram
A: 

You're trying to not instantiate your base class at any point? That's not possible; you have to explicitly or implicitly call a base constructor, whether you rewrite the field assigments or not.

It's sounds like this isn't the behavior you're looking for (because it calls base() implicitly, but then again, so does your code), but this saves you the rewrite:

public class MyObject : MyParentObject { 

int x; 
int y; 
int z; 

public MyObject() { 
    x = 5; 
    y = 10; 
} 

public MyObject(int num) : this() { 
    z = num; 
}

Why specifically do you want to avoid calling the base class constructor?

Isaac Cambron