Purpose of this

Mar 30 2006 2:28 AM

this: to represent current instance of class.
 
1st Usage: To qualify instance members otherwise hidden by parameters, as in the following:
public void SomeMethod (int hour)
{
this.hour = hour;
}
In this example, SomeMethod( ) takes a parameter (Hour) with the same name as a member
variable of the class. The this reference is used to resolve the name ambiguity. While this.Hour
refers to the member variable, Hour refers to the parameter.

2nd Usage:With indexers

3rd Usage: To pass the current object as a parameter to another method.
For instance, the following code
public void FirstMethod(OtherClass otherObject)
{
otherObject.SecondMethod(this);
}

I am not clear about 3rd usage type. What do you mean by "otherObject.SecondMethod(this);" and its usage?


Answers (1)