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
{
}
}- A derived class automatically gains all accessible members of its base class.
- 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.
- 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;
}
}- Calling the parent's constructor while adding subclass-specific initialization.
- 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.
- 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!");
}
}- A base class marks a method virtual to allow overriding; the derived class uses override to replace it.
- 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.
- 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");
}
}- A subclass can extend, rather than fully replace, the parent's behavior by calling base.Method().
- 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.
- 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