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() { }
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.
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.
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With