In C#, do you need to call the base constructor?

20,338

Solution 1

You do not need to explicitly call the base constructor, it will be implicitly called.

Extend your example a little and create a Console Application and you can verify this behaviour for yourself:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass foo = new MyClass();

            Console.ReadLine();
        }
    }

    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("BaseClass constructor called.");
        }
    }

    class MyClass : BaseClass
    {
        public MyClass()
        {
            Console.WriteLine("MyClass constructor called.");
        }
    }
}

Solution 2

It is implied, provided it is parameterless. This is because you need to implement constructors that take values, see the code below for an example:

public class SuperClassEmptyCtor
{
    public SuperClassEmptyCtor()
    {
        // Default Ctor
    }
}

public class SubClassA : SuperClassEmptyCtor
{
    // No Ctor's this is fine since we have
    // a default (empty ctor in the base)
}

public class SuperClassCtor
{
    public SuperClassCtor(string value)
    {
        // Default Ctor
    }
}

public class SubClassB : SuperClassCtor
{
    // This fails because we need to satisfy
    // the ctor for the base class.
}

public class SubClassC : SuperClassCtor
{
    public SubClassC(string value) : base(value)
    {
        // make it easy and pipe the params
        // straight to the base!
    }
}

Solution 3

It's implied for base parameterless constructors, but it is needed for defaults in the current class:

public class BaseClass {
    protected string X;

    public BaseClass() {
        this.X = "Foo";
    }
}

public class MyClass : BaseClass
{
    public MyClass() 
        // no ref to base needed
    {
        // initialise stuff
        this.X = "bar";
    }

    public MyClass(int param1, string param2)
        :this() // This is needed to hit the parameterless ..ctor
    {
         // this.X will be "bar"
    }

    public MyClass(string param1, int param2)
        // :base() // can be implied
    {
         // this.X will be "foo"
    }
}

Solution 4

It is implied.

Solution 5

A derived class is built upon the base class. If you think about it, the base object has to be instantiated in memory before the derived class can be appended to it. So the base object will be created on the way to creating the derived object. So no, you do not call the constructor.

Share:
20,338
Guy
Author by

Guy

I am a consultant specializing in TypeScript/React/JavaScript/Node.js Full Stack. I also have a strong background in C#/.Net. I am available for hire.

Updated on July 09, 2022

Comments

  • Guy
    Guy almost 2 years

    In C#, if I have an inherited class with a default constructor, do I have to explicitly call the base class' constructor or will it be implicitly called?

    class BaseClass
    {
        public BaseClass()
        {
            // ... some code
        }
    }
    class MyClass : BaseClass
    {
        public MyClass() // Do I need to put ": base()" here or is it implied?
        {
            // ... some code
        }
    }
    
  • Ty.
    Ty. over 15 years
    Looks like somebody needs a lesson in composition vs inheritance. ;)
  • Seng Cheong
    Seng Cheong about 12 years
    This is correct, because the OP stated "default (aka parameterless) constructor".
  • Wrightboy
    Wrightboy almost 9 years
    A bit old to be sure, but in case anyone stumbles across this. Note that in your overloaded constructor the call order will actually be: base() -> MyClass() -> MyClass(int param1, string param2). Your comment next to "this()" seemed somewhat misleading as if adding the this() would prevent the call to instantiate the base class.
  • Keith
    Keith almost 9 years
    @Wrightboy yes, that is the correct order in which the constructors will get called. My point was just that it isn't implied - you can explicitly specify this() or base(), but if you don't then neither will get called.
  • Wrightboy
    Wrightboy almost 9 years
    base() is always called. public MyClass(int param1, string param2) and public MyClass(int param1, string param2) : base() are equivalent. The this() just adds in additional call after the base() call but before itself.
  • iheanyi
    iheanyi about 7 years
    There is no "base" class here. This is just constructor chaining. Has little to do with the question.
  • Keith
    Keith about 7 years
    @iheanyi could you expand?
  • iheanyi
    iheanyi about 7 years
    Sorry, I edited my comment, not realizing you'd see it this quickly. This is just an example of constructor chaining. There is no base class constructor being called - because there is no inheritance.
  • iheanyi
    iheanyi about 7 years
    And, it's wrong to say if :this() was not present, there would be a call to :base(). There is no :base() to call, unless we're talking Object()
  • Keith
    Keith about 7 years
    @iheanyi sorry, I thought there being a base class could be taken as read, given the context of the question. I've expanded the example to explicitly reference a base class in order to make this clear.
  • iheanyi
    iheanyi about 7 years
    Wait. . .were you perhaps just expanding on part of the code in the question? If so, then I misunderstood your answer. Haha, again, our comments cross while writing.
  • Keith
    Keith about 7 years
    @iheanyi yeah, rather than include all the code already in the question I just expanded on the constructors to try and make a specific point. I've expanded to make it less confusing if taken out of that context.
  • variable
    variable over 4 years
    What if the base constructor has arguments, then would it still be automatically called?
  • Ian Nelson
    Ian Nelson over 4 years
    no, the base constructor is only called for parameterless constructors