哎,我就纳闷了,为啥事件的返回类型比如
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?
一个事件处理程序签名,即返回类型和它接受的参数的数量和类型,由 delegate 的签名决定
用于定义事件.所以你的例子中 Button 的 Click 事件不支持任何返回值.
An event handler signature, that is the return type and the number and types of arguments it takes, are 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.
通常,您不会期望从事件处理程序返回一个值作为函数返回值,因为一个事件可以有多个订阅者,并且每个订阅者都将独立于其他处理程序返回一个返回值,并且需要特殊的事件触发代码来决定如何处理所有返回值.
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 handlers and would require special event firing code to decide what to do with all the return values.
通常,如果您需要从事件处理程序返回通信,则 EventArgs
结构将包含处理程序可以更新的成员,并且每个处理程序将有机会查看值并相应地更新,并且触发事件的代码只需要对结构中的最终值做出反应.
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 opportunity 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!