Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating custom events - Object Sender or Typed Sender?
    primarykey
    data
    text
    <p>I searched through the archives and I found lots of questions about what sender is and why you should use the pattern but I didn't see anything about a custom event and the <em>type</em> if sender.</p> <p>Say I am creating a custom class called Subscription and it implements ISubscription and I have some event args called SubscriptionEventArgs. If Subscription had an event called Changed what is wrong about the event signature Changed(ISubscription sender, SubscriptionEventArgs e)?</p> <p>A little code to help drive the question:</p> <pre><code>public class SubscriptionEventArgs : EventArgs { // guts of event args go here } public interface ISubscription { event Action&lt;ISubscription, SubscriptionEventArgs&gt; Changed; } public class Subscription : ISubscription { public event Action&lt;ISubscription, SubscriptionEventArgs&gt; Changed; private void OnChanged(SubscriptionEventArgs e) { if (Changed!= null) { Changed(this, e); } } } </code></pre> <p>If you just despise the use of action in place of "EventHandler" then you could do the same thing but with a custom generic "EventHandler".</p> <pre><code>public delegate void EventHandler&lt;TSender, TEventArgs&gt;(TSender sender, TEventArgs e); public class SubscriptionEventArgs : EventArgs { // guts of event args go here } public interface ISubscription { event EventHandler&lt;ISubscription, SubscriptionEventArgs&gt; Changed; } public class Subscription : ISubscription { public event EventHandler&lt;ISubscription, SubscriptionEventArgs&gt; Changed; private void OnChanged(SubscriptionEventArgs e) { if (Changed!= null) { Changed(this, e); } } } </code></pre> <p>In response to Hans' request for a sample event handler:</p> <pre><code>public class SubscriptionCollection { // what is actually holding the subscriptions is not really relevant to the question private List&lt;ISubscription&gt; _subscriptions; public SubscriptionCollection() { _subscriptions = new List&lt;ISubscription&gt;(); } public void Add(ISubscription subscription) { subscription.Changed += new EventHandler&lt;ISubscription, SubscriptionEventArgs&gt;(Subscription_Changed); _subscriptions.Add(subscription); } private void Subscription_Changed(ISubscription sender, SubscriptionEventArgs e) { // Now when the subscription changed event is being handled by the collection // I don't have to look up the subscription in the list by some key and I don't // have to cast sender to the correct type because the event handler was typed // correctly from the beginning. } } </code></pre> <p>The lookup of the subscription in the list might seem trivial but what if I am working with very large sets of data and new volumes of data are coming at the application through a real-time stream. The cost of having to stop and get a reference out of a list or go through the steps of casting don't make sense. They gave us generics in 2.0 to solve that issue so I don't understand why we didn't get a generic event handler too and this led me to question what is wrong with a generic event handler?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload