为什么事件处理程序始终为void返回类型? [英] Why do event handlers always have a return type of void?

查看:122
本文介绍了为什么事件处理程序始终为void返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想知道那为什么如

Hey, I wondered why is it that the return type of events such as

private void button1_Click(object sender, EventArgs e)

总是无效?

还可以恢复任何其他值吗?

Can it return any other value too?

推荐答案

这是事件处理程序的签名,即返回类型和数量和类型的需要的参数,是由署名所确定的代表用来定义事件。所以,在你的榜样按钮的Click事件不支持任何返回值。

An event handler signature, that is the return type and the number and types of arguments it takes, is determined by the signature of the delegate used to define the event. So the Click event of the Button in your example does not support any return values.

通常情况下,你不会指望从一个事件处理程序函数的返回值返回一个值,因为一个事件可以有多个用户和其他其他处理程序的每一个将分别返回一个返回值,而且需要特殊事件触发code,以决定如何处理所有的返回值。

Typically you would not expect to return a value from an event handler as a function return value because an event can have multiple subscribers and each would be returning a return value independently of the other other handlers and would require special event firing code to decide what to do with all the return values.

通常情况下,如果你需要沟通的事件处理程序后面的EventArgs结构将包含成员,该处理器可以更新,每个处理器会得到一个oppertunity看值和相应更新,和code烧成事件只需要反应以在结构中的最终值。

Typically, if you need to communicate back from an event handler the EventArgs structure would contain members that the handler can update and each handler will get an oppertunity to look at the values and update accordingly, and the code firing the event only needs to react to the final value in the structure.

这篇关于为什么事件处理程序始终为void返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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