Codectionary / Developer documentation / C#

Inheritance & base

Inheritance lets a class (the derived or child class) reuse and extend the members of another class (the base or parent class), written as class Child : Parent. The base keyword gives access to the parent class's members from within the child, most commonly used to call the parent's constructor so the child doesn't have to duplicate its setup logic. Unlike some languages, C# only supports single class inheritance - a class can extend just one base class, but can implement multiple interfaces.

Read the syntax

class Child : Parent creates a derived class with the accessible members of a base class. : base(arguments) supplies constructor arguments to the base portion of the object.

virtual allows a base method to be overridden; override provides derived behavior. A derived override can call base.Method() when it needs to retain the parent behavior.

Syntax

class Child : Parent {\n  public Child() : base() { }\n}

Examples

Basic Inheritance

A derived class automatically gains all accessible members of its base class.

Dog rex = new Dog("Rex");
rex.Speak();  // Rex makes a sound (inherited method)

public class Animal
{
    public string Name;

    public Animal(string name)
    {
        Name = name;
    }

    public void Speak()
    {
        Console.WriteLine($"{Name} makes a sound");
    }
}

public class Dog : Animal
{
    public Dog(string name) : base(name)  // call the base constructor
    {
    }
}
  1. A derived class automatically gains all accessible members of its base class.
  2. class Child : Parent creates a derived class with the accessible members of a base class. : base(arguments) supplies constructor arguments to the base portion of the object.
  3. Dog passes Rex to Animal’s constructor and inherits Speak. Calling rex.Speak prints Rex makes a sound without declaring another Speak method in Dog.

Using base to Extend a Constructor

Calling the parent's constructor while adding subclass-specific initialization.

Cat whiskers = new Cat("Whiskers", true);
Console.WriteLine($"{whiskers.Name}, indoor: {whiskers.IsIndoor}");

public class Animal
{
    public string Name;
    public Animal(string name) { Name = name; }
}

public class Cat : Animal
{
    public bool IsIndoor;

    public Cat(string name, bool indoor) : base(name)
    {
        IsIndoor = indoor;
    }
}
  1. Calling the parent's constructor while adding subclass-specific initialization.
  2. class Child : Parent creates a derived class with the accessible members of a base class. : base(arguments) supplies constructor arguments to the base portion of the object.
  3. Cat passes the name to the Animal constructor and stores its own IsIndoor flag. The print uses both inherited Name and derived IsIndoor.

Method Overriding with virtual and override

A base class marks a method virtual to allow overriding; the derived class uses override to replace it.

Animal a = new Dog();
a.Speak();  // Woof! - the overridden version runs

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}
  1. A base class marks a method virtual to allow overriding; the derived class uses override to replace it.
  2. class Child : Parent creates a derived class with the accessible members of a base class. : base(arguments) supplies constructor arguments to the base portion of the object.
  3. Although a is declared Animal, the object is a Dog. Virtual dispatch chooses Dog.Speak at runtime, so the output is Woof!.

Calling the Base Version with base

A subclass can extend, rather than fully replace, the parent's behavior by calling base.Method().

Manager m = new Manager();
m.Work();
// Doing general work tasks
// Also managing the team

public class Employee
{
    public virtual void Work()
    {
        Console.WriteLine("Doing general work tasks");
    }
}

public class Manager : Employee
{
    public override void Work()
    {
        base.Work();  // still do the general work first
        Console.WriteLine("Also managing the team");
    }
}
  1. A subclass can extend, rather than fully replace, the parent's behavior by calling base.Method().
  2. class Child : Parent creates a derived class with the accessible members of a base class. : base(arguments) supplies constructor arguments to the base portion of the object.
  3. Manager.Work first calls Employee.Work through base, then adds a manager-specific line. Both messages appear in that order.

Best practices

  • Use : base(...) in a derived class constructor to reuse the parent's setup logic instead of duplicating it
  • Mark a base class method virtual only when you actually intend subclasses to override it - not every method needs to be overridable
  • Use base.Method() inside an override when you want to extend the parent behavior rather than fully replace it
  • Favor composition (a class containing an instance of another) over deep inheritance chains when the relationship is not truly "is-a"

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading