It's use is optional. It's used to identify the parent object's instance, and is generally used to differentiate similrly-named variables in a method:
public class XYZ
{
public int x = 5;
public void ProcessX(int x)
{
this->x += x;
}
}
In the code above, let's assume you call
ProcessX
with a value of 1. What will happen is that the variable
x
that is defined as a data memebr in the class will be incremented by the value of
x
that's passed to the method. The result will be:
this->x = 6
x = 1
Some people like to use this, some don't. Sometimes you NEED to use it (like when you're calling a method that needs a pointer to the object that's calling the method. Generally, you should name your variables appropriately to prevent any confusion, and thus eliminate most of the need to use
this
.