choose between two overloaded methods at run-time using the 'is' keyword : is « Language Basics « C# / C Sharp






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

 
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();
        }
    }
}

 








Related examples in the same category

1.Using the is Keyword to Work with an Interface
2.is Checker
3.The is operator confirms that the employee is a manager.