Can a constructors that is declared within a base class, inherited by subclasses
Yes
public class B : A
public B(int value): base(value)
using System;
public class A // This is the base class.
{
public A(int value)
{
// Executes some code in the constructor.
Console.WriteLine("Base constructor A()");
}
}
public class B : A // This class derives from the previous class.
{
public B(int value)
: base(value)
{
// The base constructor is called first.
// ... Then this code is executed.
Console.WriteLine("Derived constructor B()");
}
}
class Program
{
static void Main()
{
// Create a new instance of class A, which is the base class.
// ... Then create an instance of B.
// ... B executes the base constructor.
A a = new A(0);
B b = new B(1);
}
}
Output
Base constructor A()
Base constructor A()
Derived constructor B()