To handle an event, I use the code as below:
obj.Evt += new EvtEventHandler(DoEvt);
or
obj.Evt += DoEvt;
will make sam开发者_运维问答e result;
So... Please tell me which one I should choose! And why?
Assuming that Evt
is typed as EvtEventHandler
then there is no difference (unless you use C# 1.2, where only the first exists).
Use whichever you want.
It doesn't matter. Behind the scenes, both ways are the same (the compiler generates the same code). See "13.6 Method group conversions" in the ECMA C# specification (you can read it here).
Yes they make the same result. however if you are using 1.0
or 1.1
you must specify EvtEventHandler
, for later versions 2.0
and above you can get rid from it.
For IL, they are the same,
IL_0020: newobj instance void [mscorlib]System.EventHandler::.ctor(object,
native int)
IL_0025: callvirt instance void ConsoleApplication15.A::DoEvt(class [mscorlib]System.EventHandler)
But I think the first one is meaningful for maintaining your code.
精彩评论