C# Error: Parent does not contain a constructor that takes 0 arguments

C#InheritanceConstructorCompiler Errors

C# Problem Overview


My code is

public class Parent
{
       
    public Parent(int i)
    {
        Console.WriteLine("parent");
    }
}

public class Child : Parent
{
    public Child(int i)
    {
        Console.WriteLine("child");
    }

}

I am getting the error:

> Parent does not contain a constructor that takes 0 arguments.

I understand the problem is that Parent has no constructor with 0 arguments. But my question is, why do we need a constructor with zero arguments? Why doesn't the code work without it?

C# Solutions


Solution 1 - C#

Since you don't explicitly invoke a parent constructor as part of your child class constructor, there is an implicit call to a parameterless parent constructor inserted. That constructor does not exist, and so you get that error.

To correct the situation, you need to add an explicit call:

public Child(int i) : base(i)
{
    Console.WriteLine("child");
}

Or, you can just add a parameterless parent constructor:

protected Parent() { } 

Solution 2 - C#

You need to change your child's constructor to:

public child(int i) : base(i)
{
    // etc...
}

You were getting the error because your parent class's constructor takes a parameter but you are not passing that parameter from the child to the parent.

Constructors are not inherited in C#, you have to chain them manually.

Solution 3 - C#

You need to change the constructor of the child class to this:

public child(int i) : base(i)
{
    Console.WriteLine("child");
}

The part : base(i) means that the constructor of the base class with one int parameter should be used. If this is missing, you are implicitly telling the compiler to use the default constructor without parameters. Because no such constructor exists in the base class it is giving you this error.

Solution 4 - C#

The compiler cannot guess what should be passed for the base constructor argument. You have to do it explicitly:

public class child : parent {
    public child(int i) : base(i) {
        Console.WriteLine("child");
    }
}

Solution 5 - C#

You can use a constructor with no parameters in your Parent class :

public parent() { } 

Solution 6 - C#

By default compiler tries to call parameterless constructor of base class.

In case if the base class doesn't have a parameterless constructor, you have to explicitly call it yourself:

public child(int i) : base(i){
Console.WriteLine("child");}

Ref : Constructor calling hierarchy during inheritance

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
QuestionKuntady NitheshView Question on Stackoverflow
Solution 1 - C#dlevView Answer on Stackoverflow
Solution 2 - C#AndrewCView Answer on Stackoverflow
Solution 3 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 4 - C#Hans PassantView Answer on Stackoverflow
Solution 5 - C#AlinView Answer on Stackoverflow
Solution 6 - C#NCAView Answer on Stackoverflow