Click here to Skip to main content
15,881,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently revisiting C# and oop after a few years programming PLCs. I'm reading up on delegates and events working through a few examples. I was wondering what are the advantages of using generic event handlers over non-generic event handlers? for example,what is the benefit of using:
public event EventHandler<MyNewEventArgs> SomethingHappened

(where MyNewEventArgs is a base class of EventArgs

over
C#
public event EventHandler SomethingHappened

I've searched on the internet and not found a satisfactory explanation.

Thanks in Advance
Posted
Updated 14-Nov-14 23:23pm
v2
Comments
Richard MacCutchan 15-Nov-14 5:47am    
Please do not post the same question in multiple forums.

Signature of an event handler is usually this:
C#
void HandleThis(object sender, MyEventErgs args);

You need to create a delegate for that in order to use it with an event:
C#
public delegate MyEventHandler(object sender, MyEventErgs args);

And your event declaration is this:
C#
public event MyEventHandler MyEvent;

Imagine you have lots of different events, i.e. events with different arguments. Using a generic event halder will save you the work of declaring the delegate. You can delcare your event like this while your handler method stays the same:
C#
public event EventHandler<myeventargs> MyEvent;
</myeventargs>
 
Share this answer
 
Comments
Manas Bhardwaj 15-Nov-14 6:09am    
Yes, +5!
The Generic 'event delegate Type (available since .NET 2.0) that uses <T> where 'T may be a Type that inherits from (whose base class is) EventArgs is used when you need to pass custom data from the context in which the Event is triggered to the "consumers" of the Event who create EventHandlers for the Event and add those EventHandlers to the Invocation List of the Event.

Note that I said "'T may be:" that's not absolutely required: you can get away with using this:
C#
public class MyNewEventArgs // note: does not inherit from EventArgs
{
    public int someX = 199;

    MyNewEventArgs(int x = 999)
    {
      someX = x;
    }
}

public event EventHandler<myneweventargs> SomethingHappened;

// subscribe to the Event
private void Form1_Load(object sender, EventArgs e)
{
    SomethingHappened += Form1_SomethingHappened;
}

// EventHandler
public void Form1_SomethingHappened(object sender, Form1.MyNewEventArgs e)
{
   Console.WriteLine(e.someX);
}

// call the Event on button click
private void button1_Click(object sender, EventArgs e)
{
    SomethingHappened(this, new MyNewEventArgs(1001));
}</myneweventargs>
You could get even more concise equivalents to a generic EventHandler with custom arguments using 'Action and 'Func delegates (they are full-fledged multi-cast creatures), but generally it's always better to stick to the "classic" format of using a custom class that inherits from 'EventArgs: results in more consistent code, easier to maintain code, and code that external "consumers" of your code finds consistent with their expectations.

In a recent answer to another question here [^] I gave an example of using a Custom EventArgs based class in a generic Event definition.

That example focuses on how to use a generic Event that implements an Interface, but I think you may find some value in looking at that example and at my comments to a question from a member about that example.
 
Share this answer
 
Comments
Manas Bhardwaj 15-Nov-14 6:09am    
Well explained +5!
conradsmith 15-Nov-14 6:52am    
Thnak you very much. That's cleared it up :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900