当谈论事件处理程序时,关键字 this 在 C# 中意味着什么?

发布于 2024-11-29 05:29:31 字数 504 浏览 1 评论 0原文

当我为 csharp 编写事件处理程序时,它看起来像这样:

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

“this”关键字在这种情况下意味着什么吗?

编辑:

假设我也有这段代码:

public class GizmoManager {
    public void Manage() {
        g = new Gizmo();
        g.Foo += new EventHandler(FooHandler);
    }
}

this (在 FooHandler 内)指的是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思念绕指尖 2024-12-06 05:29:31

是的,它是对调用 FooHandler() 的对象的引用。委托能够引用静态和非静态方法。当谈论非静态时,this 是对对象实例的引用。

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

更多细节。您的代码:

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

可以像这样重写

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

在这种情况下 this 与您在处理程序中的 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

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
拿命拼未来 2024-12-06 05:29:31

更完整...

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”将引用 Bar 的一个实例

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

厌倦 2024-12-06 05:29:31

this 将引用您当前所在的类,而不是方法。

来自 MSDN

this 关键字引用该类的当前实例。静止的
成员函数没有 this 指针。 this 关键字可以是
用于从构造函数、实例方法和
实例访问器。

在您的示例中,this.doSomething() 引用该方法之外的某个任意类中的方法。 这个是多余的。

在这种情况下使用 this 很有用:

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

它有助于描述含义。否则,如果没有this,您真正指的是什么namealias

最后,sender 将引用引发事件的object

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文