Consider the following code:
public class Vehicle
{
public void StartEngine()
{
// Code here.
}
}
public class CityBus : Vehicle
{
public void MoveToLocation(Location location)
{
////base.StartEngine();
this.StartEngine();
// Do other stuff to drive the bus to the new location.
}
}
Is there any difference between this.StartEngine(); and base.StartEngine();, except that in the second case, StartEngine method cannot be moved to or overridden in CityBus class? Is there a performance impact?
The only difference is an explicit call to look at your parent class versus an implicit one that ends up in the same place through simple inheritance. Performance difference is negligible. like Hans Passant said, a call to base.StartEngine() will cause weird behavior if you make StartEngine virtual at some point.
You shouldn’t need either qualifier to get the the right place.
this.StartEngine()is almost always redundant when explicitly coded. You may have code that indirectly puts athisreference in a list of objects, but then it’s the reference in the list being called: