[英]C# EventHandler Question
Quick question regarding EventHandlers in C#, let's say we have the following code: 关于C#中的EventHandlers的快速提问,假设我们有以下代码:
MyObject.MyEventHandler += (...)
I am currently refactoring some code, and the (...) is often replaced with another eventhandler, as such : 我目前正在重构一些代码,并且(...)通常被另一个事件处理程序替换,例如:
EventHandler A;
Test()
{
A += A_Method;
MyObject.MyEventHandler += A
}
Wouldn't it be simpler to disregard "A" and just write instead: 忽略“ A”而不是简单地写起来会不会更简单:
Test()
{
MyObject.MyEventHandler += A_Method;
}
What is the use of EventHandler "A", if we can just directly pass the method to the EventHandler object from "MyObject" ? 如果我们可以直接将方法从“ MyObject”传递给EventHandler对象,那么EventHandler“ A”的用途是什么?
Thanks ! 谢谢 !
Sure, as long as A
isn't used other places. 当然,只要不在其他地方使用
A
Otherwise it might have been a refactoring to reduce code duplication. 否则可能是为了减少代码重复而进行的重构。
I assume you mean 我想你是说
A += A_Method;
MyObject.MyEventHandler += A;
(without parentheses after A_Method). (在A_Method之后没有括号)。 If so, assuming that there is nothing more complex around this than the example,
A
can probably be safely omitted. 如果是这样的话,假设没有比这个例子更复杂的情况,则可以安全地省略
A
When refactoring, F12 (go to definition) is your friend: find all references and make sure they all are properly re-routed, etc. 重构时,F12(转到定义)是您的朋友:查找所有引用,并确保所有引用都正确地进行了路由等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.