Polymorphism & dynamic dispatch
Understand polymorphism: call virtual members through a base reference, use base-typed collections, avoid method hiding, and cast safely when needed.
Polymorphism & dynamic dispatch is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Polymorphism overview
Goal: Use one base type and let derived types customize behavior.
- Base reference, derived object
- virtual + override → dynamic dispatch
- Base-typed collections
- Avoid new method hiding
Dynamic dispatch basics
Calling a virtual method uses the object’s runtime type, not the variable’s compile-time type.
using System;
public class Animal
{
public virtual string Speak() { return "???"; }
}
public class Cat : Animal
{
public override string Speak() { return "Meow"; }
}
public class Dog : Animal
{
public override string Speak() { return "Woof"; }
}
public class Program
{
public static void Main(string[] args)
{
Animal a1 = new Cat(); // base reference
Animal a2 = new Dog();
Console.WriteLine(a1.Speak()); // Meow
Console.WriteLine(a2.Speak()); // Woof
}
}
Polymorphism in collections
Store different derived types in a List<Base> and call virtual members uniformly.
using System;
using System.Collections.Generic;
public class Shape
{
public virtual double Area() { return 0.0; }
}
public class Rectangle : Shape
{
private readonly int _w, _h;
public Rectangle(int w, int h) { _w = w; _h = h; }
public override double Area() { return _w * _h; }
}
public class Circle : Shape
{
private readonly int _r;
public Circle(int r) { _r = r; }
public override double Area() { return 3.14159 * _r * _r; }
}
public class Program
{
public static void Main(string[] args)
{
List<Shape> shapes = new List<Shape>();
shapes.Add(new Rectangle(3, 4));
shapes.Add(new Circle(2));
foreach (Shape s in shapes)
{
Console.WriteLine("Area = " + s.Area()); // calls correct override
}
}
}
Hiding vs override
Avoid method hiding (new). Use override for real polymorphism; hiding calls the base version via a base reference.
using System;
public class Printer
{
public virtual void Print() { Console.WriteLine("[Base]"); }
}
public class LoudPrinter : Printer
{
public override void Print() { Console.WriteLine("[LOUD]"); }
}
public class HiddenPrinter : Printer
{
// Hides the base method instead of overriding (BAD fit for polymorphism)
public new void Print() { Console.WriteLine("[HIDDEN]"); }
}
public class Program
{
public static void Main(string[] args)
{
Printer a = new LoudPrinter();
Printer b = new HiddenPrinter();
a.Print(); // [LOUD] (override works)
b.Print(); // [Base] (hiding fails via base reference)
}
}
Safe casting (as + check)
Prefer base/virtual calls. If you must use derived-only members, cast safely (as + null check).
using System;
public class Vehicle
{
public virtual string Info() { return "Vehicle"; }
}
public class Car : Vehicle
{
public bool HasAirConditioning;
public override string Info() { return "Car"; }
}
public class Program
{
public static void Main(string[] args)
{
Vehicle v = new Car();
Console.WriteLine(v.Info()); // polymorphic call
// Need a Car-only member? Cast carefully using 'as'
Car c = v as Car;
if (c != null)
{
c.HasAirConditioning = true;
Console.WriteLine("AC set: " + c.HasAirConditioning);
}
else
{
Console.WriteLine("Not a Car");
}
}
}
Polymorphism tips
Tips:
- Design for base/virtual calls first.
- Use override, not new, to change behavior.
- Keep base APIs small and stable.
- Cast rarely; prefer polymorphism or interfaces.
Dynamic dispatch meaning
Recap
Recap: Polymorphism lets one base call reach many derived behaviors via virtual/override. Use base-typed collections, avoid new hiding, and cast only when necessary.
Frequently asked questions
Is the “Polymorphism & dynamic dispatch” lesson free?
Yes — the full text of “Polymorphism & dynamic dispatch” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Polymorphism & dynamic dispatch”?
Understand polymorphism: call virtual members through a base reference, use base-typed collections, avoid method hiding, and cast safely when needed. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Polymorphism & dynamic dispatch” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- virtual/override/sealed; abstract classes
- Interfaces (intro) & multiple interface implementation
- Polymorphism & dynamic dispatch