Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The virtual and override keywords

Reference for C# virtual, override and sealed override: how virtual dispatch works, rules for overriding, covariant returns and common mistakes.

virtual marks a method, property, indexer or event of a class as overridable. A derived class replaces it with override. When code calls the member through a base-class variable, the runtime picks the implementation that matches the actual type of the object, not the type of the variable. This is run-time polymorphism, also called virtual dispatch.

Members are non-virtual by default in C#. A base class has to opt in with virtual (or abstract), which makes overriding a deliberate design decision.

C#
class Animal { public virtual string Sound() => "..."; }
class Dog : Animal { public override string Sound() => "Woof"; }

Try it

C# Calling through a base reference

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.

C# base calls, sealed override and covariant returns

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

C#
public virtual ReturnType Name(parameters) { ... }
public override ReturnType Name(parameters) { ... }
public sealed override ReturnType Name(parameters) { ... }
public virtual Type Property { get; set; }
Modifier Description
virtual Declares a member with a default implementation that derived classes may replace.
override Replaces an inherited virtual, abstract or override member. Signature, accessibility and (apart from covariant returns) return type must match.
sealed override Overrides and stops further overriding in classes that derive from this one.
abstract A member with no body that every concrete derived class must override (see abstract).

Errors: overriding a non-virtual member is CS0506; override with no matching base member is CS0115; changing the access level in an override is CS0507; combining virtual with private, static or sealed is rejected by the compiler.

Usage notes

  • Dispatch uses the runtime type. A method call through Shape s = new Circle(2) runs Circle.Area(). Hiding with new does not do this: the variable's type picks the method.
  • Same signature required. Parameter types, ref/out modifiers and accessibility must match. Default parameter values are not inherited by overrides; the value of the static type of the call is used, which surprises many people. Avoid changing defaults in overrides.
  • Calling the parent. base.Method() runs the next implementation up the chain. It is a normal, non-virtual call.
  • Do not call virtual members from a constructor. The base constructor runs before the derived constructor, so the override can see fields that are not yet initialized.
  • Covariant returns. Since C# 9 an override of a method or read-only property may return a more derived type than the base declaration. Parameter types stay invariant.
  • Properties and events can also be virtual; for an auto-property each override replaces the accessors you list.
  • Structs cannot declare virtual members, but they can override ToString, Equals and GetHashCode from object.
  • Cost. A virtual call is an indirect call through the type's method table. It is cheap, but it prevents inlining unless the JIT can prove the exact type. Sealed classes and sealed overrides help it do so.
  • Records synthesize virtual Equals, GetHashCode, ToString and a virtual Clone-like copy member; derived records override them automatically.