❔ C# EventArgs
I'm starting to use System.EventHandler events and I'm just wondering if there is a point to the EventArgs class other than to be inherited for custom args? Like, I'm calling
StartEvent?.Invoke(this, new EventArgs());
and the new EventArgs()
just seems kind of pointless since its empty. Can I send something through the default EventArgs with a lambda expression or something?44 Replies
You can use
EventArgs.Empty
to avoid an allocation
But yeah, it's just there so you can add some args in the future without breaking backwards compatAnd to add those args, you have to make a class deriving from EventArgs?
What is the event?
So
I have these guys
So you own the event?
Yes
Really you shouldn't be using just plain EventArgs
If I don't want to send any args, just use EventArgs.Empty like the other person said?
And if I do want to send args, make a class deriving from EventArgs with the values I need?
If the event doesn't need any args then the type of the event should be
Action
oh
If you need args, you should use
Action<T1, T2, ...>
With
Action
s, can you still reference the sender in the functions added to it?Well you can add that as an argument
ah
Like this?
What's the point of passing an
object
as the sender?
Do the subscribers actually need to do anything with the sender?So a
PlayerAbility
is invoking these events and I want the UIManager
to be able to update with these values with when the event fires. I send the PlayerAbility
sender with the event so that the subscribers can get the public values they need.
(The UIManager
subscribes some of its update functions to the ability events)Would this be a better choice then?
Since the sender would only ever be a PlayerAbility?
yeah
Generally having to cast an EventArgs to the appropriate derived type and having the sender as an object is just bad
Yeah that's what I've been doing and it just feels bad
Thank you for your help :)
Eh, the standard advice isn't to use Action etc for events. That's very rare
The thing with using EventHandler is that you can add extra args layer without breaking backwards compat. That's why all of Microsoft's events use the event args pattern
And yes, so add your own args, make your own EventArgs subclass
If you pass things by
ref
or in
, use a delegate typeAlthough you shouldn't pass just
EventArgs
Why not? That's why EventHandler and EventArgs.Empty exist, and for exactly the same reason: so that you can add args without a breaking change
Well you have to cast them, which is inherently not very safe
No you don't?
I mean a type deriving from EventArgs sure, not just plain EventArgs
I don't get the confusion. If you have an EventHandler event and a subscriber which takes EventArgs, you can update the event to be an EventHander<T> without breaking the subscriber, and later update the subscriber to take that EventArgs subclass
No casting required
This is of course assuming the subscriber doesn't do anything with the args
There's no reason not to use EventHandler<T> from the start
EventArgs<T>? That's not a thing
What do you mean by that? What would they do with the instance of EventArgs.Empty?
I meant EventHsndler<T>
EventHandler<EventArgs> is the same as the non generic EventHandler
The latter is effectively shorthand for the former
EventHandler<YourEventArgs>
Yes. But if you don't need any args at the moment, you can just use the base class EventArgs
And that's forward compatible for when you do make your own subclass
sure
although I don't think you should be specifically planning for "when you eventually add arguments"
I mean, that's the whole point of the pattern
Forwards compatibility
For adding some / more new args
sure
whatever floats your boat
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.