Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Polymorphism in C#

Reference for C# polymorphism: virtual and override dispatch, abstract members, hiding with new, base calls, interface dispatch and how the runtime picks a method.

Polymorphism means that code written against a general type runs the behavior of the specific object it receives. If a variable of type Shape refers to a Circle, then calling shape.Area() runs the circle's version. The caller never checks which kind of shape it has.

C# offers two sources of polymorphism: inheritance with virtual/abstract members that derived classes override, and interfaces that unrelated classes implement. In both cases the method is chosen at run time from the real type of the object (dynamic dispatch), not from the declared type of the variable.

C#
Shape s = new Circle(1);
Console.WriteLine(s.Area());   // runs Circle.Area, because Area is virtual

Try it

C# Virtual and override

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# Override versus new

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#
class Base
{
    public virtual void M() { }          // may be replaced
    public abstract void N();            // (in an abstract class) must be replaced
}

class Derived : Base
{
    public override void M() { }         // replaces Base.M
    public sealed override void N() { }  // replaces, and stops further overriding
    public new void P() { }              // hides a member; not polymorphic
}
Modifier Description
virtual Marks a method, property, indexer or event as replaceable. It has a body.
abstract Declares a member with no body in an abstract class. Every concrete derived class must override it.
override Replaces an inherited virtual, abstract or overriding member. Signature, accessibility and (nearly always) return type must match.
sealed override Overrides and prevents any further class from overriding it.
new Hides an inherited member with the same name. The base member is still reached through base-typed variables.

Errors: overriding a member that is not virtual gives CS0506; leaving out override when a name matches an inherited member gives warning CS0108 (hiding); a concrete class that does not implement an abstract member is error CS0534.

Usage notes

  • Dispatch depends on the object, not the variable. A virtual call uses the run-time type. A non-virtual call, including a new member, uses the compile-time type of the expression.
  • Virtual calls are cheap but not free. Each class has a method table; a virtual call is an indirect call that the JIT can sometimes devirtualize, especially for sealed classes. Make classes sealed when you do not design them for inheritance.
  • Do not call virtual methods from constructors. The base constructor runs before the derived constructor, so the override can see a half-built object with fields at their defaults.
  • Covariant return types. Since C# 9 an override may return a more derived type than the base method, for example a Clone() override that returns Dog instead of Animal.
  • Use base.M() to extend rather than replace the inherited behavior.
  • Interfaces give polymorphism without a shared base class. Any class may implement IGreeter; see abstract classes vs. interfaces.
  • Prefer polymorphism over is ladders when the set of kinds is open. For a closed set of cases, a switch expression with patterns can be clearer.
  • Overloads are not polymorphism. Overload resolution happens at compile time from the static argument types.