Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Event Best Practice - Expose via property or return in EventArgs [closed]

Tags:

c#

class

events

I'm unsure about the best practice for event handling/class design.

I have a class that has a method Discover(). When the method completes it raises an event, DiscoveryCompleted. The DiscoveryCompleted event has EventArgs.

In the Discover() method a List of objects are created. Should these be returned when the DiscoveryCompleted event fires in the EventArgs (will still need to create a class that derives from EventArgs), or be set as a property (ReadOnlyCollection) on the class. I have a bool Discovered which is set when the DiscoveryCompleted fires, so this can be used to determine if data should be populated.

like image 668
Henno Avatar asked Sep 13 '25 08:09

Henno


1 Answers

If the data is either transitive (where the value might change after the event has fired and you want subscribers to be able to view the data that pertains to that particular event) or not directly related to your component (the result of an async call that the component won't use itself later), then return it in the event args.

Otherwise, if it would make sense to expose the value as a property on the component (disregarding your event), then do that and don't worry about the EventArgs. If not, then include it in the args.

like image 167
Adam Robinson Avatar answered Sep 14 '25 20:09

Adam Robinson