choose between two overloaded methods at run-time using the 'is' keyword

image_pdfimage_print
   
 
using System;

public class BankAccount {
    virtual public void Withdraw() {
        Console.WriteLine("Call to BankAccount.Withdraw()");
    }
}

public class SavingsAccount : BankAccount {
    override public void Withdraw() {
        Console.WriteLine("Call to SavingsAccount.Withdraw()");
    }
}

public class MainClass {

    public static void Main(string[] strings) {
        BankAccount ba = new BankAccount();
        Test(ba);

        SavingsAccount sa = new SavingsAccount();
        Test(sa);
    }
    public static void Test(BankAccount baArgument) {
        if (baArgument is SavingsAccount) {
            SavingsAccount saArgument = (SavingsAccount)baArgument;
            saArgument.Withdraw();
        } else {
            baArgument.Withdraw();
        }
    }
}