c# 事件 +=和-=有什么区别。

一个事件可以有很多的侦听者挂接在上面,这些侦听者通过注册自己的事件处理例程来告诉系统说,当这个事件发生的时候请调用我的xxx方法。
具体到你这里的代码,OutputDataReceived 就是一个事件,侦听者想要告诉系统说,当我收到outputdata的时候请你执行我的encoder_OutputDataReceived方法。 侦听者怎么样用程序的语言告诉系统呢,就是你这里写好的 += 语句了,
process_xt.OutputDataReceived += new DataReceivedEventHandler(encoder_OutputDataReceived);

那有一天,侦听者可能不感兴趣这个事件了, 那么他可以取消掉自己的注册。 用 -= 咯
process_xt.OutputDataReceived -= new DataReceivedEventHandler(encoder_OutputDataReceived);

猜你喜欢

转载自blog.csdn.net/u010294743/article/details/77995464