C# Base Keyword: Usage and Examples
In C#, the base keyword is used to call methods or properties of the base class. By using the base keyword, a subclass can invoke the corresponding methods or properties of the base class when overriding them. Here is an example of how the base keyword is used:
- Invoke the base class constructor.
 
public class BaseClass
{
    public BaseClass(int value)
    {
        // 构造函数的逻辑
    }
}
public class SubClass : BaseClass
{
    public SubClass(int value) : base(value)
    {
        // 子类构造函数的逻辑
    }
}
SubClass sub = new SubClass(10);
- invoke the methods of the base class:
 
public class BaseClass
{
    public void SomeMethod()
    {
        // 方法逻辑
    }
}
public class SubClass : BaseClass
{
    public void AnotherMethod()
    {
        base.SomeMethod(); // 调用基类方法
        // 方法逻辑
    }
}
- Accessing parent class properties:
 
public class BaseClass
{
    public int BaseProperty { get; set; }
}
public class SubClass : BaseClass
{
    public void SomeMethod()
    {
        int value = base.BaseProperty; // 获取基类属性的值
        // 方法逻辑
    }
}
By using the “base” keyword, a subclass can easily reuse the implementation logic of the base class and customize it when needed.