CodeBlanks Logo Icon CodeBlanks

Summary - Polymorphism

C# course · Intermediate · Lesson 64/116

Combine virtual, override and pattern matching to write an employee hierarchy.

Summary: the polymorphism toolkit

In this group you met four key polymorphism tools: virtual/override for runtime dispatch, new for method hiding, is/as for type testing and safe casts, and switch expressions with type patterns.

Now you will combine them in a real scenario: an employee hierarchy. First you will compute salary with virtual dispatch, then distribute bonuses by position with a switch expression. The same code will behave differently for different subtypes; that is the power of polymorphism.

Exercise Compute salary with virtual dispatch

Define Employee with public virtual int Pay() that does return 1000;. Define Manager : Employee with override int Pay() that does return 2000;. In Main build a List<Employee> with new Manager() and new Employee(). In a foreach print each Pay() via Console.WriteLine.

Show example solution
Employee { public override int Pay() { return 2000; } }. In Main build List<Employee> { new Manager(), new Employee() } and foreach (var e in emps) Console.WriteLine(e.Pay());
Example solution:
using System;
using System.Collections.Generic;

class Employee
{
    public virtual int Pay()
    {
        return 1000;
    }
}

class Manager : Employee
{
    public override int Pay()
    {
        return 2000;
    }
}

class Program
{
    static void Main()
    {
        var emps = new List<Employee> { new Manager(), new Employee() };
        foreach (var e in emps)
        {
            Console.WriteLine(e.Pay());
        }
    }
}

Exercise Give bonuses with a switch expression

Reuse the Employee and Manager classes above. In Program write static int Bonus(Employee e) using a switch expression: return 500 for Manager, 100 for Employee. In Main call Bonus with a Manager and an Employee, printing each result with Console.WriteLine.

Show example solution
Write static int Bonus(Employee e) { return e switch { Manager m => 500, _ => 100 }; }. In Main add Console.WriteLine(Bonus(new Manager())); Console.WriteLine(Bonus(new Employee()));
Example solution:
using System;

class Employee { }
class Manager : Employee { }

class Program
{
    static int Bonus(Employee e)
    {
        return e switch
        {
            Manager m => 500,
            _ => 100
        };
    }

    static void Main()
    {
        Console.WriteLine(Bonus(new Manager()));
        Console.WriteLine(Bonus(new Employee()));
    }
}

Polymorphism is yours

Congratulations! You now know the four polymorphism tools: virtual/override, new, is/as and switch expressions. You can manage many different subtypes through a single base reference and let runtime select the right behavior.

In the next groups you will combine these tools with collections, generic classes and LINQ. Polymorphism is a load-bearing pillar of object-oriented C#; most modern libraries are built on these mechanisms.

Test yourself

  1. Which performs runtime dispatch?

    Employee e = new Manager();
    e.CalculatePay();
    • Hiding with new
    • A virtual + override call
    • A static method call
    • A sealed class call
    Show answer

    A virtual + override call — Correct. The virtual + override pair calls Manager.CalculatePay at runtime.

  2. The operator for a type pattern

    if (e ____ Manager m) { m.Bonus = 1000; }
    Show answer
    if (e is Manager m) { m.Bonus = 1000; }

All lessons in Intermediate