Question

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
    }
}
Was it helpful?

Solution

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.");
        }
    }
}

OTHER TIPS

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!
    }
}

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"
    }
}

It is implied.

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.

AFAIK, you only need to call the base constructor if you need to pass down any values to it.

You don’t need call the base constructor explicitly it will be implicitly called, but sometimes you need pass parameters to the constructor in that case you can do something like:

using System;
namespace StackOverflow.Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            NewClass foo = new NewClass("parameter1","parameter2");
            Console.WriteLine(foo.GetUpperParameter());
            Console.ReadKey();
        }
    }

    interface IClass
    {
        string GetUpperParameter();
    }

    class BaseClass : IClass
    {
        private string parameter;
        public BaseClass (string someParameter)
        {
            this.parameter = someParameter;
        }

        public string GetUpperParameter()
        {
            return this.parameter.ToUpper();
        }
    }

    class NewClass : IClass
    {
        private BaseClass internalClass;
        private string newParameter;

        public NewClass (string someParameter, string newParameter)
        {
            this.internalClass = new BaseClass(someParameter);
            this.newParameter = newParameter;
        }

        public string GetUpperParameter()
        {
            return this.internalClass.GetUpperParameter() + this.newParameter.ToUpper();
        }
    }
}

Note: If someone knows a better solution please tells me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top