By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,770 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,770 IT Pros & Developers. It's quick & easy.

Reasoning behind the EventHandler(object, EventArgs) signature

P: n/a
What is the reasoning for recommending that all events have the
signature EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which is much easier IMO.

Am I missing something?

Cheers
JB
Jan 30 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I guess for the same reasons that any good architect will impose some
sort of order on any framework they design.

The signature reassures the developer that a certain behaviour is
expected and that the sender of the event and some kind of event
argument, either raw or derived, will be provided. A void return type is
also imposing order and suggesting that info will be passed back in the
event arguments.

Creating your own delegates is perfectly acceptable but, if you were
writing any sort of commercial software, the choice would be nonsense
because it wouldn't fit with the plan.

Systems that use reflection to discover the capabilities of a component,
and there are lots of them these days, are also aware of the signature
so things like databinding use these events in the data change
notification system.

You would be strongly advised not to break what doesn't need fixing.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

John B wrote:
What is the reasoning for recommending that all events have the
signature EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which is much easier IMO.

Am I missing something?

Cheers
JB
Jan 30 '07 #2

P: n/a
On Jan 30, 4:00 am, John B <jbngs...@yahoo.comwrote:
What is the reasoning for recommending that all events have the
signature EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which is much easier IMO.

Am I missing something?
Well, prior to 2.0, you weren't. I often wondered the same thing -
it's not like you could use a less specific event handling delegate as
a more specific one. However, with 2.0 delegates can be covariant, so
it makes a lot more sense. I'm still not 100% convinced, but there's
more reason than there was before :)

Jon

Jan 30 '07 #3

P: n/a
This reasoning is from the .NET SLAR:

"
If we want to add some new information in the event callback we can do it
without breaking clients. For example, if we need to add information about
the location an assembly was loaded from we would simply add a new property
to the AssemblyLoadEventArgs class. If we did not have the EventArgs class
we would have to create a whole new Delegate and a new event of that type
with a different name. It would be really ugly. Encapsulating the event data
in a subclass of EventArgs makes this scenario very smooth.
"

I do agree, if you are making a class library or components for external
use. Having a common base class is good (see Exception) choice.
You can pass your derived (extended) class to relevant standard events if
needed, as they expect EventArgs derived class.
Another question are the standards, the common ways to do things. When all
events have EventArgs (and sender), something different can disturb.

"John B" <jb******@yahoo.comha scritto nel messaggio
news:9w*******************@nasal.pacific.net.au...
What is the reasoning for recommending that all events have the signature
EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each event
instead of creating the delegate which is much easier IMO.

Am I missing something?

Cheers
JB

Jan 30 '07 #4

P: n/a
Thanks for the responses guys.
Excellent information and viewpoints.

Cheers
JB
Jan 30 '07 #5

P: n/a
On Jan 30, 3:31 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Well, prior to 2.0, you weren't. I often wondered the same thing -
it's not like you could use a less specific event handling delegate as
a more specific one. However, with 2.0 delegates can becovariant, so
it makes a lot more sense. I'm still not 100% convinced, but there's
more reason than there was before :)

Jon
Jon,

Did you mean contravariant? EventHandler has a void return type so I
don't think that would be in play. The parameters would though,
right?

Why doesn't C# support covariant return types on virtual methods in
subclasses? I thought I read somewhere that a newer version of Java
does.

Brian
Jan 31 '07 #6

P: n/a
Brian Gideon <br*********@yahoo.comwrote:
Did you mean contravariant? EventHandler has a void return type so I
don't think that would be in play. The parameters would though,
right?
Yup. I'm a fool :)
Why doesn't C# support covariant return types on virtual methods in
subclasses? I thought I read somewhere that a newer version of Java
does.
I can't remember, to be honest. I know it was one of the possible side-
effects of putting generics in. I agree, it would be very nice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.