C
C#17mo ago
Pacrombie

❔ 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
canton7
canton717mo ago
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 compat
Pacrombie
Pacrombie17mo ago
And to add those args, you have to make a class deriving from EventArgs?
Thinker
Thinker17mo ago
What is the event?
Pacrombie
Pacrombie17mo ago
So
Pacrombie
Pacrombie17mo ago
Pacrombie
Pacrombie17mo ago
I have these guys
Thinker
Thinker17mo ago
So you own the event?
Pacrombie
Pacrombie17mo ago
Pacrombie
Pacrombie17mo ago
Yes
Thinker
Thinker17mo ago
Really you shouldn't be using just plain EventArgs
Pacrombie
Pacrombie17mo ago
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?
Thinker
Thinker17mo ago
If the event doesn't need any args then the type of the event should be Action
Pacrombie
Pacrombie17mo ago
oh
Thinker
Thinker17mo ago
If you need args, you should use Action<T1, T2, ...>
Pacrombie
Pacrombie17mo ago
With Actions, can you still reference the sender in the functions added to it?
Thinker
Thinker17mo ago
Well you can add that as an argument
Pacrombie
Pacrombie17mo ago
ah
Pacrombie
Pacrombie17mo ago
Pacrombie
Pacrombie17mo ago
Pacrombie
Pacrombie17mo ago
Like this?
Thinker
Thinker17mo ago
What's the point of passing an object as the sender? Do the subscribers actually need to do anything with the sender?
Pacrombie
Pacrombie17mo ago
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)
Pacrombie
Pacrombie17mo ago
Pacrombie
Pacrombie17mo ago
Would this be a better choice then? Since the sender would only ever be a PlayerAbility?
Thinker
Thinker17mo ago
yeah Generally having to cast an EventArgs to the appropriate derived type and having the sender as an object is just bad
Pacrombie
Pacrombie17mo ago
Yeah that's what I've been doing and it just feels bad Thank you for your help :)
canton7
canton717mo ago
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
Anton
Anton17mo ago
If you pass things by ref or in, use a delegate type
Thinker
Thinker17mo ago
Although you shouldn't pass just EventArgs
canton7
canton717mo ago
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
Thinker
Thinker17mo ago
Well you have to cast them, which is inherently not very safe
canton7
canton717mo ago
No you don't?
Thinker
Thinker17mo ago
I mean a type deriving from EventArgs sure, not just plain EventArgs
canton7
canton717mo ago
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
Thinker
Thinker17mo ago
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
canton7
canton717mo ago
EventArgs<T>? That's not a thing What do you mean by that? What would they do with the instance of EventArgs.Empty?
Thinker
Thinker17mo ago
I meant EventHsndler<T>
canton7
canton717mo ago
EventHandler<EventArgs> is the same as the non generic EventHandler The latter is effectively shorthand for the former
Thinker
Thinker17mo ago
EventHandler<YourEventArgs>
canton7
canton717mo ago
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
Thinker
Thinker17mo ago
sure although I don't think you should be specifically planning for "when you eventually add arguments"
canton7
canton717mo ago
I mean, that's the whole point of the pattern Forwards compatibility For adding some / more new args
Thinker
Thinker17mo ago
sure whatever floats your boat
Accord
Accord17mo ago
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.