145

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?

1
  • I had the same error message for another reason when extending an abstract class with an internal constructor from another assembly. This is currently the case for TypeInfo, FieldInfo, ... in DotNetCore. Just wanted to share this knowledge as it took me hours to resolve it.
    – Grochni
    Aug 20, 2015 at 6:19

6 Answers 6

206

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() { } 
3
  • 9
    Probably a good idea to create a protected parent() { } instead of public.
    – Roberto
    Aug 17, 2015 at 5:51
  • @Roberto Can you elaborate why? Aug 30, 2016 at 19:33
  • 3
    @JuanCarlosOropeza The basic reason is that, up until now, there was no need for a parameterless parent constructor, so the assumption would be that consumers of the class don't need one. Since we really only want one for the sake of any deriving classes, protected is the sensible modifier, since that has the exact semantics of "only accessible to child classes".
    – dlev
    Aug 30, 2016 at 21:20
35

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.

3
  • Nitpicking: He isn't getting any exception. That's a compiler error he is getting Aug 29, 2011 at 13:15
  • 3
    @DanielHilgarth you are technically correct, the best kind of correct. Dec 13, 2013 at 13:39
  • guys seems in .net Core FaultException<T> is missing one,two and three param constructors. Wondering , if any one resolved issue. I am migrating old code to .net core
    – Taran
    Aug 22, 2018 at 20:10
19

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.

11

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

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

public parent() { } 
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.