Constructors can useBaseKeyword to call the constructor of the base class. For example:
Public ClassMANAGER: Employee
{
PublicManager (IntAnnualsalary)
:Base(Annualsalary)
{
//Add further instructions here.
}
}
In this example, the base class constructor is called before the constructor block is executed.BaseThe keyword can be used with or without parameters. Any parameters of the constructor can be usedBaseOr as part of the expression. For more information, seeBase (C # reference).
In a derived class, if you do not useBaseIf a base class constructor is explicitly called by a keyword, the default constructor is called implicitly (if any ). This means that the following constructor Declaration has the same effect:
PublicManager (IntInitialdata)
{
//Add further instructions here.
}
PublicManager (IntInitialdata)
:Base()
{
//Add further instructions here.
}
If the base class does not provide the default constructor, the derived class must useBaseExplicitly call the base constructor.
You can use the this keyword to call another constructor in the same object. AndBaseSame,ThisCan be used with or without parameters. Any parameters in the constructor can be usedThisOr as part of the expression. For example, you can useThisOverride the second constructor in the previous example:
PublicEmployee (IntWeeklysalary,IntNumberofweeks)
:This(Weeklysalary * numberofweeks)
{
}
In the previous exampleThisThis constructor is called because of the Keyword:
PublicEmployee (IntAnnualsalary)
{
Salary = annualsalary;
}