Previous messages:
Event signature in .NET - using a strong typed "Sender"?
In a C # event handler, why should the sender parameter be an object?
Microsoft conventions and recommendations force .NET users to use a special template for creating, creating and processing events in .NET.
Event Design Guides http://msdn.microsoft.com/en-us/library/ms229011.aspx states that
Citation:
The following conventions are observed in the signature of the event handler:
The return type is Void.
The first parameter is called the sender and is of type Object. This is the object that raised the event.
The second parameter is called e and is of type EventArgs or a derived class of EventArgs. This is event-specific data.
The method takes exactly two parameters.
These conventions tell developers that the following (next) shorter and more obvious code is evil:
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; }
and (the following) longer and less obvious code is good:
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; }
Although these recommendations do not indicate why it is so important to follow these conventions, forcing developers to act like monkeys who do not know why and what they are doing.
IMHO, Microsoft's event logging conventions for .NET are bad for your code because they cause extra effort with zero efficiency in coding, coding, coding:
- Coding "(MyObject) sender" (not to mention 99% of situations that do not require a sender at all)
- Encoding retrieved from "MyEventArgs" for data passed inside an event handler.
- Coding splits (calling "e.MyData" when data is required instead of "data")
It is not so difficult to do this, but in practical terms, what we lose when we disagree with Microsoft agreements, except that people perceive you as a heretic because your act of confrontation with Microsoft agreements borders on blasphemy :)
Do you agree?