Keywords
Access Keywords
base
base
The base
keyword is used to access members of the base class from within a derived class.
Call a method on the base class that has been overridden by another method.
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. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/
Specify which base-class constructor should be called when creating instances of the derived class.
public class BaseClass {
int num;
public BaseClass(){
Console.WriteLine("in BaseClass()");
}
public BaseClass(int i) {
num = i;
Console.WriteLine("in BaseClass(int i)");
}
}
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)
*/
Contextual Keywords
partial
partial
Partial type definitions allow for the definition of a class, struct, interface, or record to be split into multiple files.
namespace PC
{
partial class A
{
int num = 0;
void MethodA() { }
partial void MethodC();
}
partial class A
{
void MethodB() { }
partial void MethodC() { }
}
}
All the parts must use the partial
keyword, and must have the same accessibility, such as public
, private
, and so on. All the parts must be available at compile time to form the final type.
The partial
keyword isn't allowed on constructors, finalizers, overloaded operators, property declarations, or event declarations.
Last updated
Was this helpful?