Back to questions
JuniorTheoryCommon
What does the program print?
Vehicle v = new Taxi();
Console.WriteLine(v.Who());
class Vehicle { public virtual string Who() => "V"; }
class Car : Vehicle { public override string Who() => "C"; }
class Taxi : Car
{
public override string Who() => "T" + base.Who();
}TC
- Expecting
V: the variable has typeVehicle, butWhois virtual, so the version comes from the object's type,Taxi. - Expecting
TVorTCV:base.Who()runs only the version of the direct base classCar, andCar.Whodoes not callVehicle.Who.
- What does the program print if
Taxihas noWhomethod of its own? - What happens at compile time if
virtualis removed fromVehicle.Who?