Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between 'base' and 'this' when referring to the parent object field, property or method?

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?

like image 751
Arseni Mourzenko Avatar asked Sep 01 '10 02:09

Arseni Mourzenko


1 Answers

No difference, StartEngine() isn't virtual. You should not use base, in case you ever refactor it to make it virtual. The perf diff is not measurable.

like image 100
Hans Passant Avatar answered Sep 28 '22 08:09

Hans Passant