Base keyword is used to access the base class from the derived class:
Methods of rewritten by other methods on the base class.
Specifies the base class constructor that should be called when the party instance is created.
Base class access can only be performed in a constructor, an instance method, or an instance attribute accessor.
It is wrong to use the base key from the static method.
Example
In this example, the base class Person and derived class Employee have a method called getInfo. By using the base keyword, you can call the GetInfo method on the base class from the derived class.
// Keywords_Base.cs
// Accessing Base Class Members
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 {
Public static void
Main
()
{
Employee E = New Employee ();
E.GetInfo ();
}
}
Output
Name: John L. Malgraine
SSN: 444-55-6666
EMPLOYEE ID: ABC567EFG
For other examples, see New, Virtual, and Override.
Example
This example shows how to specify the base class constructor that is called when the party instance is created.
// Keywords_Base2.cs
Using system;
Public class mybase
{
Int Num;
Public mybase ()
{
Console.WriteLine ("in mybase ()");
}
Public MyBase (INT i)
{
Num = i;
Console.writeline ("In MyBase (INT I)");
}
Public int getnum ()
{
Return Num;
}
}
Public class myderived: mybase
{
// this constructor will call mybase.mybase ()
Public myderived (): Base ()
{
}
// this constructor will call mybase.mybase (INT i)
Public Myderived (INT I): Base (i)
{
}
Public static void
Main
()
{
Myderived md = new myderived ();
Myderived md1 = new myderived (1);
}
}
Self-written a small program:
Using system;
Namespace ConsoleApplication6
{
///
/// Class1 summary description.
/// summary>
Class test
{
///
/// The main entry point for the application.
/// summary>
[Stathread]
Static void
Main
(String [] ARGS)
{
//
// Todo: Add code here to start the application
//
Person objPerson = New Person ();
Objperson.name = "eNet";
ObjPerson.old = 18;
ObjPerson.frondof = "Watch TV";
Objperson.displayselfinfo ();
Console.writeline ("===================================================== === ");
Boy objboy = new boy ();
Objboy.name = "eNet";
Objboy.old = 24;
Objboy.frondof = "Play Basketball";
Objboy.girlfriendname = "annney";
Objboy.displayselfinfo ();
}
}
Public Class Person
{
protected string name = ""
protected int = 1;
protected string frondof = ""
Public String Name
{
set
{
Name = value;
}
}
Public Int Old
{
set
{
Old = Value;
}
}
Public String Frondof
{
set
{
Frondof = Value;
}
}
Public Virtual Void DisplaySelfinfo ()
{
Console.writeline ("My Name is: {0}, and I am {1} Years Old Now, And i am frond of {2}!", Name, old, frondof);
}
}
Public Class Boy: Person
{
Protected String GirfriendName = "";
Public String GirlfriendName
{
set
{
Girlfriendname = value;
}
}
Public override void displayselfinfo ()
{
Base.displayselfo ();
Console.writeline ("My Girlfriend's Name IS {0}, She is Very Beautiful, I Love Her, And She Love Me Too, We Happy Every Day!", GirlFriendName
}
}