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.

Syntax

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

Examples

Basic Inheritance

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

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
    {
    }
}

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

Using base to Extend a Constructor

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

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;
    }
}

Cat whiskers = new Cat("Whiskers", true);
Console.WriteLine($"{whiskers.Name}, indoor: {whiskers.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.

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

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}

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

Calling the Base Version with base

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

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");
    }
}

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

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"

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Classes & Objects
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.
Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.
Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.
Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.