How one constructor call another constructor?

To call one C# constructor from another, before the body of the constructor, use either:

: base (parameters)

to call a constructor in the base class; or:

: this (parameters)

to call a constructor in this class.

The following examples illustrate how to call one constructor from another.

public class Supervisor : Employee
{
public Supervisor (int vacationDays) : base (vacationDays)
{
//Add more statements here.
}
}

In the prededing example, the base class constructor is called before the constructor block is executed. The base keyword is usable with or without parameters. Any constructor parameters can be used either as part of an expression or as parameters to base.

public class Employee
{
public Employee(int weeklyOvertime)
{
overtime = weeklyOvertime;
}

public Employee (int overtimeRate, int numberHours)
: this (overtimeRate * numberHours)
{
//Add more statements here.
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *