This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of Most Base -> Most Derived.
So in your particular instance, it calls Person()
, and then Customer()
in the constructor orders. The reason why you need to sometimes use the base
constructor is when the constructors below the current type need additional parameters. For example:
public class Base
{
public int SomeNumber { get; set; }
public Base(int someNumber)
{
SomeNumber = someNumber;
}
}
public class AlwaysThreeDerived : Base
{
public AlwaysThreeDerived()
: base(3)
{
}
}
In order to construct an AlwaysThreeDerived
object, it has a parameterless constructor. However, the Base
type does not. So in order to create a parametersless constructor, you need to provide an argument to the base constuctor, which you can do with the base
implementation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…