I was just trying to master the concept of virtual function using a console app. I noticed as soon as I override a base class function, return baseclassname.functionname(parameters) gets inserted in my function body automatically. Why does this happen?
class advanc
{
public virtual int calc (int a , int b)
{
return (a * b);
}
}
class advn : advanc
{
public override int calc(int a, int b)
{
//automatically inserted
return base.calc(a, b);
}
}
By overriding a virtual function you expand the functionality of your base class and then call the base class for the ‘base functionality’.
If you would remove the ‘
return base.calc(a,b)‘ line, the base class code would not execute.If you want to completely replace the functionality this is not a problem but if you want to extend the functionality then you should also call the base class.
The following code demonstrates this (just put it in a Console Application)