Home:ALL Converter>Is there a difference between the following ways to register to event

Is there a difference between the following ways to register to event

Ask Time:2009-08-11T22:18:36         Author:Elad

Json Formatter

I would like to register to some event. The following ways works:

public void AddOptionAsListner(OptionElement option)
    {
        option.Selected += onOptionSelectedChanged;
    }

public void AddOptionAsListner(OptionElement option)
    {
        option.Selected += new EventHandler(onOptionSelectedChanged);
    }

Is there a difference or that this is just different syntax for the same thing?

Author:Elad,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1260669/is-there-a-difference-between-the-following-ways-to-register-to-event
Gishu :

Same - No diff. The compiler infers the type of delegate and does it auto-magically for you. Syntactic sugar to make your life a bit easier\n\nJust checked with C#-in depth. This feature is called \"Method group conversions\" ; added in C#2.0\n\ne.g. from the book\n\nstatic void MyMethod() { ... }\nstatic void MyMethod( object sender, EventArgs e) {...}\n\nstatic void Main() {\n ThreadStart x = MyMethod; // binds to first overload\n EventHandler y = MyMethod; // binds to second overload\n}\n\n\nIf I open this up in reflector, you'd see that the compiler just created the delegate instances of the right type for you, behind the scenes of course.\n\n L_0000: ldnull \n L_0001: ldftn void CS.Temp.Program::MyMethod()\n L_0007: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object, native int)\n L_000c: pop \n L_000d: ldnull \n L_000e: ldftn void CS.Temp.Program::MyMethod(object, class [mscorlib]System.EventArgs)\n L_0014: newobj instance void [mscorlib]System.EventHandler::.ctor(object, native int)\n L_0019: pop \n",
2009-08-11T14:21:26
Brandon :

It's the same thing, the new EventHandler() is just redundant.\n\nYou don't need to explicitly create a delegate for an event handler.",
2009-08-11T14:20:50
yy