The this
keyword is a reference to the current instance of the class.
In your example, this
is used to reference the current instance of the class Complex
and it removes the ambiguity between int real
in the signature of the constructor vs. the public int real;
in the class definition.
MSDN has some documentation on this as well which is worth checking out.
Though not directly related to your question, there is another use of this
as the first parameter in extension methods. It is used as the first parameter which signifies the instance to use. If one wanted to add a method to the String class
you could simple write in any static class
public static string Left(this string input, int length)
{
// maybe do some error checking if you actually use this
return input.Substring(0, length);
}
See also: http://msdn.microsoft.com/en-us/library/bb383977.aspx