CHAPTER 5 UNDERSTANDING ENCAPSULATION
public void SetDriverName(string name)
{
this.name = name;
}
Do understand that if there is no ambiguity, you are not required to make use of the this keyword
when a class wishes to access its own data fields or members, as this is implied. For example, if we
rename the string data member from name to driverName (which will also require you to update your
Main() method) the use of this is optional as there is no longer a scope ambiguity:
class Motorcycle
{
public int driverIntensity;
public string driverName;
public void SetDriverName(string name)
{
// These two statements are functionally the same.
driverName = name;
this.driverName = name;
}
...
}
Even though there is little to be gained when using this in unambiguous situations, you might still
find this keyword useful when implementing class members, as IDEs such as SharpDevelop and Visual
Studio will enable IntelliSense when this is specified. This can be very helpful when you have forgotten
the name of a class member and want to quickly recall the definition. Consider Figure 5-2.
Figure 5-2. The IntelliSense of this
171