Note that there are some explanatory texts on larger screens.

plurals
  1. POCode-friendly version of event signature in .NET
    primarykey
    data
    text
    <p>Preceding posts:</p> <p><a href="https://stackoverflow.com/questions/1046016/event-signature-in-net-using-a-strong-typed-sender">Event Signature in .NET — Using a Strong Typed 'Sender'?</a></p> <p><a href="https://stackoverflow.com/questions/1437699/in-a-c-event-handler-why-must-the-sender-parameter-be-an-object">In a C# event handler, why must the “sender” parameter be an object?</a></p> <hr> <p>Microsoft's conventions and guidelines force .NET users to use special pattern for creating, raising and handling events in .NET.</p> <p>Event design guidelines <a href="http://msdn.microsoft.com/en-us/library/ms229011.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms229011.aspx</a> state that</p> <hr> <p>Citation:</p> <p>The event-handler signature observes the following conventions :</p> <ul> <li><p>The return type is Void.</p></li> <li><p>The first parameter is named sender and is of type Object. This is the object that raised the event.</p></li> <li><p>The second parameter is named e and is of type EventArgs or a derived class of EventArgs.This is the event-specific data.</p></li> <li><p>The method takes exactly two parameters.</p></li> </ul> <hr> <p>These conventions tell developers that the (following) shorter and more obvious code is evil:</p> <pre><code>public delegate void ConnectionEventHandler(Server sender, Connection connection); public partial class Server { protected virtual void OnClientConnected(Connection connection) { if (ClientConnected != null) ClientConnected(this, connection); } public event ConnectionEventHandler ClientConnected; } </code></pre> <p>and the (following) longer and less obvious code is good:</p> <pre><code>public delegate void ConnectionEventHandler(object sender, ConnectionEventArgs e); public class ConnectionEventArgs : EventArgs { public Connection Connection { get; private set; } public ConnectionEventArgs(Connection connection) { this.Connection = connection; } } public partial class Server { protected virtual void OnClientConnected(Connection connection) { if (ClientConnected != null) ClientConnected(this, new ConnectionEventArgs(connection)); } public event ConnectionEventHandler ClientConnected; } </code></pre> <p>Though these guidelines not state why is it so important to follow these conventions, making developers act like monkeys who don't know why and what are they doing.</p> <p>IMHO, Microsoft's event signature conventions for .NET are bad for your code because they cause additional zero-efficiency effort to be spent on coding, coding, coding:</p> <ol> <li>Coding "(MyObject)sender" casts (not speaking about 99% of situations that don't require sender at all)</li> <li>Coding derived "MyEventArgs" for the data to be passed inside event handler.</li> <li>Coding dereferences (calling "e.MyData" when the data is required instead of just "data")</li> </ol> <p>It's not that hard to do this effort, but practically speaking what are we loosing when not conforming to Microsoft's conventions, except that people take you as an heretic because your act of confrontation to Microsoft's conventions <a href="https://stackoverflow.com/questions/1046016/event-signature-in-net-using-a-strong-typed-sender">verges on blasphemy</a> :)</p> <p>Do you agree?</p>
    singulars
    1. This table or related slice is empty.
    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