The base keyword is used to access the members of the base class from the derived class:
Base Class access can only be performed in constructors, instance methods, or instance attribute accessors.
It is incorrect to use the base keyword in a static method.
using System;public class Person{protected string ssn = "444-55-6666";protected string name = "John L. Malgraine";public virtual void GetInfo(){Console.WriteLine("Name: {0}", name);Console.WriteLine("SSN: {0}", ssn);}}class Employee : Person{public string id = "ABC567EFG";public override void GetInfo(){// Calling the base class GetInfo method:base.GetInfo();Console.WriteLine("Employee ID: {0}", id);}}class TestClass{static void Main(){Employee E = new Employee();E.GetInfo();}}
Output:
Name: John L. MalgraineSSN: 444-55-6666Employee ID: ABC567EFG
This example shows how to specify the base class constructor called when creating a derived class instance.
Using system;
Public class baseclass
{
Int num;
Public baseclass ()
{
Console. writeline ("in baseclass ()");
}
Public baseclass (int I)
{
Num = I;
Console. writeline ("in baseclass (INT I )");
}
Public int getnum ()
{
Return num;
}
}
Public class derivedclass: baseclass
{
// This constructor will call baseclass. baseclass ()
Public derivedclass (): Base ()
{
}
// This constructor will call baseclass. baseclass (int I)
Public derivedclass (int I): Base (I)
{
}
Static void main ()
{
Derivedclass MD = new derivedclass ();
Derivedclass md1 = new derivedclass (1 );
}
}
Output
in BaseClass()in BaseClass(int i)