By overriding a virtual function, you extend the functionality of your base class, and then call the base class for "base functionality."
If you delete the line ' return base.calc(a,b)
', the base class code will not be executed.
If you want to completely replace functionality, this is not a problem, but if you want to extend functionality, you must also call the base class.
The following code demonstrates this (just put it in a console application)
class advanc { public virtual int calc(int a, int b) { Console.WriteLine("Base function called"); return (a * b); } } class advn : advanc { public bool CallBase { get; set; } public override int calc(int a, int b) { Console.WriteLine("Override function called"); if (CallBase) { return base.calc(a, b); } else { return a / b; } } } private static void Main() { advn a = new advn(); a.CallBase = true; int result = a.calc(10, 2); Console.WriteLine(result); a.CallBase = false; result = a.calc(10, 2); Console.WriteLine(result); Console.WriteLine("Ready"); Console.ReadKey(); }
source share