Csharp/CSharp Tutorial/Class/base
a subclass uses the keyword base to invoke a particular subclass constructor
using System;
public class Class1 {
public static void Main(string[] args) {
SubClass sc1 = new SubClass();
SubClass sc2 = new SubClass(1, 2);
}
}
public class BaseClass {
public BaseClass() {
Console.WriteLine("Constructing BaseClass (default)");
}
public BaseClass(int i) {
Console.WriteLine("Constructing BaseClass({0})", i);
}
}
public class SubClass : BaseClass {
public SubClass() {
Console.WriteLine("Constructing SubClass (default)");
}
public SubClass(int i1, int i2)
: base(i1) {
Console.WriteLine("Constructing SubClass({0}, {1})",
i1, i2);
}
}
Use base keyword to access field in the base class
using System;
class BaseClass
{
public string Field1 = "In the base class";
}
class DerivedClass : BaseClass
{
new public string Field1 = "In the derived class";
public void Display()
{
Console.WriteLine("{0}", Field1); // Access the derived class.
Console.WriteLine("{0}", base.Field1); // Access the base class.
}
}
class Program
{
static void Main()
{
DerivedClass oc = new DerivedClass();
oc.Display();
}
}
In the derived class In the base class
Using base to Access a Hidden Name
"base" refers to the base class of the derived class in which it is used.
This usage has the following general form:
base.member