C# Practice Exercises

Polymorphic Payroll

Calculate pay for different employee types.

Introduction

An abstract base class lets payroll code treat every employee uniformly.

Runtime dispatch calls the correct Pay implementation for each derived employee.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Linq;
abstract class Employee { public abstract decimal Pay(); }
class Salaried(decimal salary) : Employee { public override decimal Pay() => salary; }
class Hourly(decimal rate, int hours) : Employee { public override decimal Pay() => rate * hours; }
class Program
{
    static void Main()
    {
        Employee[] staff = { new Salaried(1000), new Hourly(20, 10) };
        Console.WriteLine(staff.Sum(employee => employee.Pay()));
    }
}
Result of running the code
1200

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges