+= 带有事件的运算符 [英] += operator with Events

查看:27
本文介绍了+= 带有事件的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Bar()
{
    Foo foo = new Foo();
    **foo.MyEvent += foo_MyEvent;**
    foo.FireEvent();        
}

void foo_MyEvent(object sender, EventArgs e)
{
    ((Foo)sender).MyEvent -= foo_MyEvent;
}

嘿,我对事件有点陌生,有人能告诉我 += 运算符对事件做了什么吗?

Hey I'm a bit unfamiliar with events, could someone tell me what the += operator does with events?

推荐答案

+= 订阅事件.+= 右侧的委托或方法将被添加到事件跟踪的内部列表中,并且当所属类触发该事件时,列表中的所有委托将被调用.

+= subscribes to an event. The delegate or method on the right-hand side of the += will be added to an internal list that the event keeps track of, and when the owning class fires that event, all the delegates in the list will be called.

这篇关于+= 带有事件的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆