Original address: Https://msdn.microsoft.com/zh-cn/library/hfw7t1ce.aspx
The base keyword is used to access the members of the base class from a derived class:
Base class access can only be done in constructors, instance methods, or instance property accessors.
It is wrong to use the base keyword from a static method (where the condition estimates are consistent with the This keyword, because static methods are at the class level and not at the object level).
The base class that is accessed is the base class specified in the class declaration. For example, if you specify class Classb:classa, the members of ClassA are accessed from ClassB regardless of the base class of ClassA.
Example
In this case, both the base class person and the derived class Employee have a method named Getinfo. you can call the base class's Getinfo method from a derived class by using the base keyword .
C#
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 (); }}/*outputname:john L. Malgrainessn:444-55-6666employee id:abc567efg*/
For additional examples, see new, virtual, and override.
Example
This example shows how to specify the base class constructor that is called when a derived class instance is created.
C#
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 would call Baseclass.baseclass () public DerivedClass (): Base () { } //This constructor would 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) */
Base (C # Reference)