开发者

C# EventHandler Question

开发者 https://www.devze.com 2023-02-04 01:38 出处:网络
Quick question regarding EventHandlers in C#, let\'s say we have the following code: MyObject.MyEventHandler += (...)

Quick question regarding EventHandlers in C#, let's say we have the following code:

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:

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" ?

Thanks !


I assume you mean

A += A_Method;
MyObject.MyEventHandler += A;

(without parentheses after A_Method). If so, assuming that there is nothing more complex around this than the example, A can probably be safely omitted. When refactoring, F12 (go to definition) is your friend: find all references and make sure they all are properly re-routed, etc.


Sure, as long as A isn't used other places. Otherwise it might have been a refactoring to reduce code duplication.

0

精彩评论

暂无评论...
验证码 换一张
取 消