Information Technology Reference
In-Depth Information
Base Access
Sometimes, your derived class might need to access a hidden inherited member. You can access
a hidden base class member by using a
base access
expression. This expression consists of the
keyword
base
, followed immediately by a period and the name of the member, as shown here.
Console.WriteLine("{0}",
base.Field1
);
↑
Base access
For example, in the following code, derived class
OtherClass
hides
Field1
in its base class
but accesses it by using a base access expression.
class SomeClass { // Base class
public string Field1 = "Field1 -- In the base class";
}
class OtherClass : SomeClass { // Derived class
new public string Field1 = "Field1 -- In the derived class";
↑
↑
Hides the field in the base class
public void PrintField1()
{
Console.WriteLine("{0}", Field1); // Access the derived class.
Console.WriteLine("{0}",
base.Field1)
; // Access the base class.
}
↑
}
Base access
class Program {
static void Main()
{
OtherClass oc = new OtherClass();
oc.PrintField1();
}
}
This code produces the following output:
Field1 -- In the derived class
Field1 -- In the base class