为什么事件处理程序的返回类型总是 void?

Why do event handlers always have a return type of void?(为什么事件处理程序的返回类型总是 void?)
本文介绍了为什么事件处理程序的返回类型总是 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?

推荐答案

一个事件处理程序签名,即返回类型和它接受的参数的数量和类型,由 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)