When I write an event handler for csharp, it looks like this:
public void FooHandler(object sender, EventArgs e)
{
//do stuff..
this.doSomething(); //Does the "this" keyword mean something in this context?
}
Does the “this” keyword mean something in this context?
EDIT:
Let’s say I also have this code:
public class GizmoManager {
public void Manage() {
g = new Gizmo();
g.Foo += new EventHandler(FooHandler);
}
}
What would the this (within FooHandler) refer to?
Yes, it’s a reference to object for which
FooHandler()is called. Delegates are capable of referencing both static and non-static methods. When talking about non-static ones,thisis a reference to object instance.Some more details. Your code:
could be re-written like this
In this case
thisis the samethisthat you have in your handler 😉And even more, if you have some problems with understanding
this: