C# Reference ยท Classes and Object-Oriented Concepts
Inheritance
Reference for C# inheritance: deriving classes, base and protected, virtual and override, abstract and sealed members, new hiding, casting and constructor chaining.
Inheritance lets a derived class reuse and extend a base class. The derived class gets the accessible members of its base and can add new ones or change inherited behavior. C# supports single inheritance of classes: a class has exactly one base class (System.Object if you name none), but it can implement many interfaces.
The relationship should read as "is a": a Dog is an Animal. A reference of the base type can hold a derived object, and calling a virtual member through it runs the derived override. That is polymorphism.
public class Animal { public virtual string Sound() => "..."; } public class Dog : Animal { public override string Sound() => "Woof"; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
class Derived : Base, IInterface { } // base class first, then interfaces sealed class Leaf : Base { } // cannot be inherited class Base { public virtual void M() { } // may be overridden public abstract void N(); // (in an abstract class) must be overridden protected int shared; // visible to derived classes } class Derived : Base { public override void M() => base.M(); // replace or extend public new void Other() { } // hide an inherited member }
| Keyword | Meaning |
|---|---|
virtual |
Declares that derived classes may override the member. |
override |
Replaces an inherited virtual, abstract or override member. Signature and accessibility must match. |
abstract |
A member without body (or a class that cannot be instantiated). Derived concrete classes must implement it. |
sealed |
On a class: no derived classes. On an override: no further overrides. |
new |
Deliberately hides an inherited member (the base method is still reachable through a base reference). |
base |
Refers to the base class: base.M(), base(args) in a constructor. |
protected |
Visible in the class and in derived classes. |
Usage notes
- Constructors are not inherited. A derived class must call a base constructor, implicitly the parameterless one or explicitly with
: base(args). virtualis opt-in. Methods are non-virtual by default, which keeps calls fast and predictable. Only mark membersvirtualwhen you design them to be overridden.overrideversusnew: override changes behavior for every call, whatever the variable's declared type.newonly hides the member when accessed through the derived type. Accidental hiding triggers warning CS0114 or CS0108, a sign you probably meantoverride.- Prefer composition to deep hierarchies. If the relationship is "has a" or you only want to reuse code, hold an object in a field instead. Deep inheritance chains are brittle: changes in the base can break all derived types.
- Liskov substitution: a derived object must work wherever the base type is expected without surprising callers. Don't override a method to throw
NotSupportedExceptionunless the base documents it. - Casting: an upcast (derived to base) is implicit and always safe. A downcast needs
as,isor a cast, and a failed cast throwsInvalidCastExceptionunless you useasoris. - Structs cannot inherit from classes or structs; they can implement interfaces. Records can inherit from other records.
sealedclasses are slightly faster to call into and communicate that inheritance was not designed for; seal types unless you intend extension.- Everything derives from
object, which providesToString,Equals,GetHashCodeandGetTypethat you may override.