Mega Code Archive

 
Categories / C# / Language Basics
 

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