I think there is an error in the following page https://msdn.microsoft.com/en-us/library/ms173115.aspx about constructors and invoking base constructors right after the following paragraph:
"The use of the this keyword in the previous example causes this constructor to be called:"
public Employee(int annualSalary)
{
salary = annualSalary;
}
Shouldn't it be like this?
public Employee(int weeklySalary, int numberOfWeeks)
{
salary = weeklySalary * numberOfWeeks;
}
Can anyone confirm? thanks a lot
Nope, it's showing you the source of the constructor is being called by this(weeklySalary * numberOfWeeks)
... and that's the single-parameter constructor which just assigns to the salary
variable.
Think of the code as being like this:
// Constructor X
public Employee(int weeklySalary, int numberOfWeeks)
: this(weeklySalary * numberOfWeeks)
{
}
// Constructor Y
public Employee(int annualSalary)
{
salary = annualSalary;
}
And then the documentation as:
The expression
this(weeklySalary * numberOfWeeks)
in constructor X indicates a chained call to constructor Y.
See more on this question at Stackoverflow