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

C#InheritanceConstructor

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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 - C#

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 - C#

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 - C#

It is implied.

Solution 5 - C#

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.

Solution 6 - C#

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

Solution 7 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuyView Question on Stackoverflow
Solution 1 - C#Ian NelsonView Answer on Stackoverflow
Solution 2 - C#Rob CooperView Answer on Stackoverflow
Solution 3 - C#KeithView Answer on Stackoverflow
Solution 4 - C#John DowneyView Answer on Stackoverflow
Solution 5 - C#Tom WelchView Answer on Stackoverflow
Solution 6 - C#Lars MæhlumView Answer on Stackoverflow
Solution 7 - C#jl23xView Answer on Stackoverflow