Reputation: 736
When I skip the identifier with delegate type, the compiler throws error saying identifier required. So, when declaring delegate, why one has to specify the identifier of type? Having only the type information is enough in the declaration right?
public delegate void MyDel(object o, EventArgs e); // accepted by compiler
public delegate void MyDel(object, EventArgs); // throws error, why?
NOTE: C++ supports declarations with only types. As I'm coming from C++ background, I expected the same behavior here.
Upvotes: 4
Views: 884
Reputation: 14007
There's at least two good reasons that come to my mind immediately:
Consistency with method declarations
You are right that the parameter name is not part of the signature and as such it is technically not required to match the delegate (consequently, the parameter names are ignored when matching the delegate to a method). However, consistency is an important feature of a language. It makes learning it easier and reduces cognitive workload, which in turn increases productivity. A delegate is a "placeholder" for a method. To be consistent, it makes sense to make its definition as similar to a method definition as possible. A method would be declared like this:
void PropertyChangedHandler(object sender, PropertyChangedEventArgs e) {
//...
}
A delegate to this method can be defined like:
delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);
As you can see, the only difference is the delegate
keyword (and of course the lack of a method body, which is irrelevant here, because that's not part of the signature). That's easy to learn and remember.
Developement aids
In Visual Studio you can type the event name, then +=
, press Tab twice and the event handler method will be generated for you. The names of the delegate's parameters are used for the generated method. If the delegate would only come with parameter types and not names, the parameters would have to be named param1
, param2
etc., which wouldn't be very meaningful. The same applies to other development aids, for example when you write code to invoke the delegate, IntelliSense will show you the names of the delegate's parameters. That's much more useful than just their types.
Upvotes: 1
Reputation: 2561
There is also the problem of named calls to the method
MyDel myDel = MyMethod;
myDel(o:sender,e: eve);
c# allows it and if you didn't have a name how could you do this.
Upvotes: 1
Reputation: 272760
Note that I can't find anything official about this. The following is my guess.
First of all, having parameter names makes it easy for people to know what that parameter does. If you have just object
instead of object sender
, it is very ambiguous what is the importance of this parameter. If you put the word sender
there, people will know on first sight that this parameter represents the sender of an event.
Secondly, this makes it easy for the IDE to generate code for you. Ever tried making the Windows Forms Designer generate an event handler for you? It generates the parameter names according to the parameters in the delegate declaration. If you don't put parameter names in the declaration, the IDE can't generate meaningful names for you.
And lastly, keeping this syntax similar to that of method declarations is probably less work for the compiler developers. :)
Upvotes: 0
Reputation: 543
Any good programmer will define the parameter names clearly indicating what they are for. If only type is allowed then it leads to confusion. This confusion needs documentation and careful reading of it leading to loss of productivity. For this designers of language decided to have parameter names to be defined when defining a delegate or an interface.
Imagine SQLCommand defn
SqlCommand(string, SqlConnection)
vs
SqlCommand(string cmdText, SqlConnection connection)
Upvotes: -1
Reputation: 239764
If nothing else, so that whilst you're writing the documentation you can clearly indicate which of the parameters you're discussing. (E.g. especially for delegates with multiple parameters of the same type)
It's also consistent with other areas (such as abstract methods or interface methods) that also have no body, but still require the parameters to be named.
Upvotes: 1