Favicon

Base access keyword

Peponi12/9/20242m

C#
SyntaxKeywordAccess

1. Introduction

base 키워드는 상속받은 클래스의 멤버에 액세스하는 데 사용한다. 상속받은 클래스의 생성자, 메서드, 인스턴스 속성 호출이 가능하며 static 메서드에서는 상속받은 클래스 호출이 불가능하다.

2. Example

Base class
public class Base
{
    public virtual bool Property { get; set; } = false;
 
    public Base(bool property)
    {
        Property = property;
        Console.WriteLine($"Base ctor - {Property}");
    }
 
    public virtual void Method() => Console.WriteLine("Base Method");
}
Derived class
public class Derived : Base
{
    public Derived(bool property) : base(property) => Console.WriteLine($"Derived ctor - {property}");
 
    public override bool Property { get => base.Property; set => base.Property = value; }
 
    public override void Method()
    {
        base.Method();
        Console.WriteLine($"Derived Method - {Property}");
    }
 
    // public static void DerivedMethod() => base.Method();  // CS1511
}
Main.cs
static void Main()
{
    Derived d = new Derived(true);
    d.Method();
}
 
/* output:
Base ctor - True
Derived ctor - True
Base Method
Derived Method - True
*/

3. 참조 자료