Reputation:
Just following some tutorial(http://www.techotopia.com/index.php/Hiding_and_Showing_Forms_in_C_Sharp) which says:
As mentioned in the previous section, in order to remove a form both from the display, and from memory, it is necessary to Close rather than Hide it. In Visual Studio double click, once again, the button in subForm to view the Click event procedure. Once again, because the button is in the form we are closing we need to use this instead of subForm when calling the Close() method:
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
I still don't get reason why to use this.Close()
here, as opposed to just Close()
?
(smth similar is being said about Hide()
method, one section above).
Upvotes: 1
Views: 150
Reputation: 3784
this
is used for the current instance field/properties/methods/constructors/events etc. Therefore just using Close()
would help.
However in cases where you might have more than one field/object within a field, it might not invoke/use the object one. In that case you have to explicitly state this
An example would be
void someMethod(FooClass someClass) {
this.someClass = someClass; //assigning the local someClass field to the current object's someClass field.
}
Upvotes: 1
Reputation: 11051
From what i understand in the tutorial, its not about the this
, its about making clear that the current window is being closed/hidden. Besides that, this
is for some people a matter of style, to make it even clearer that this instance is meant. It is optional. Unless you need a way to pass the current instance to a method for example, than you need this
.
Upvotes: 0
Reputation: 1062650
In most cases, that will not be necessary. Since it isn't conflicting with any local variables (because there aren't any local variables), the only time it would be required is if Close()
is an extension method available to the current instance. However, some people prefer to add it for "clarity" (I'm not sure it adds any, personally).
As a minor note: here's an example of when it would be ambiguous due to a local:
Action Close = delegate { Console.WriteLine("hello"); }; // or whatever
this.Close(); // calls the instance method or extension method
Close(); // invokes the local delegate
Finally, there are cases where this.Foo()
is desirable because base.Foo()
or otherObject.Foo()
is also used in the same method, and it is desirable to keep them obvious. For example, in a .Equals(...)
or .CompareTo(...)
method.
Upvotes: 4
Reputation: 56697
In this case this
can actually be omitted. this
means simply "the current instance of the class". There are cases when this
is necessary, for example when an instance variable has the same name as a local variable. In that case, this
must be used to access the instance variable instead of the local one.
Example:
public class Test
{
private int i;
public Test(int i)
{
this.i = i; // Sets the instance variable
i = 1; // Sets the local variable
}
}
EDIT: Marc Gravell correctly mentions extension methods, which only function on instances. So to call some method on the current instance of a class you must use this
, too.
Upvotes: 10