开发者

What does the keyword this mean in C#, when talking about event handlers?

When I write an event handler for csharp, it looks like this:

public void F开发者_运维百科ooHandler(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, this is a reference to object instance.

class A
{
   public delegate void MyDelegate(object sender, int x);
   public event MyDelegate TheEvent;

   public void func()
   {
     if(TheEvent != null) TheEvent(this, 123);
   }
}

class B
{
   public B()
   {
     A a = new A();
     a.TheEvent += handler;
     a.func();
   }

   public void handler(object sender, int x)
   {
      // "sender" is a reference to object of type A that we've created in ctor
      // "x" is 123
      // "this" is a reference to B (b below)
   } 
}

B b = new B(); // here it starts

Some more details. Your code:

g = new Gizmo();
g.Foo += new EventHandler(FooHandler);

could be re-written like this

g = new Gizmo();
g.Foo += new EventHandler(this.FooHandler); // look here

In this case this is the same this that you have in your handler ;-)

And even more, if you have some problems with understanding this:

class X
{
  int a;

  public X(int b)
  {
    this.a = b; // this stands for "this object"
    // a = b is absolutely the same
  }

  public X getItsThis()
  {
    return this;
  }
}

X x = new X();
X x2 = x.getItsThis();
// x and x2 refer to THE SAME object
// there's still only one object of class X, but 2 references: x and x2


more complete...

public class Bar
{
  public Bar()
  {
    Gizmo g = new Gizmo();
    g.Foo += new EventHandler(FooHandler);

  }

  public void FooHandler(object sender, EventArgs e)
  {
    //do stuff..
    this  //Does the "this" keyword mean something in this context?
  }
}

"this" will refer to an instance of Bar


this is going to refer to the current class you are in, not the method.

From MSDN,

The this keyword refers to the current instance of the class. Static member functions do not have a this pointer. The this keyword can be used to access members from within constructors, instance methods, and instance accessors.

In your example, this.doSomething() refers to a method in some arbitrary class outside of that method. this is redundant.

It is useful to use this in cases like this:

public Employee(string name, string alias) 
{
   this.name = name;
   this.alias = alias;
}

It helps delineate between the meaning. Otherwise, without this what name or alias are you really referring to?

Finally, sender is going to refer to the object which raised the event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜