Do we have any advantage or is it just tradition to call base class constructor like #1,
do both #1 and #2 differ? (I have used #2 when I happen to have overloading constructor for BaseClass, but why we ).
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("From Base");
}
}
#1
public class DerivedClass
: BaseClass
{
public DerivedClass()
:base()
{
Console.WriteLine("From Derived");
}
}
#2
public class DerivedClass
: BaseClass
{
public DerivedClass()
{
Console.WriteLine("From Derived");
}
}
No, there is no difference. In case #2, the compiler will generate the call to the base class constructor (if there is a parameterless base class constructor available, that is). Adding such a call manually is a style question. It is only compulsory when there is no parameterless base class constructor available, so the compiler can’t decide itself how to call it.