base keyword in c#
In c#, to access members and functionalities of the base class, the ‘base’ keyword is used within a derived class. In the given code, we have used ‘base’ keyword to call the constructor of the Account class.
class Account
{
private string mCode;
private string mName;
private string mDescription;
private double mBalance;
public Account(string code, string name, string description, double balance)
{
mCode = code;
mName = name;
mDescription = description;
mBalance = balance;
}
public Account()
{
}
In the below code, the derived class constructor – ‘PartyAccount’ receives the values first and which are then passed to the base class constructor.
class PartyAccount :Account
{
private string mAddress;
private string mPhone;
public PartyAccount(string code, string name, string description, double balance, string address, string phone)
: base(code, name, description, balance)
{
mAddress = address;
mPhone = phone;
}
public PartyAccount()
: base()
{
}
Note : The ‘MyBase’ keyword in vb.net is the counterpart for the ‘base’ keyword in c#.
No comments:
Post a Comment