Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By design why is it mandatory to specify parameter names when declaring delegate type?

Tags:

c#

Why must we specify the parameter name x as follows

public delegate void XXX(int x);

when declaring a delegate type?

For me, the parameter name x is unused so it will be simpler if we can rewrite as follows:

public delegate void XXX(int);

Please let me know why the C# designer "forced" us to specify the parameter names.

Edit1:

Is public delegate TResult Func<T1,TResult>(T1 arg1) more readable than public delegate TResult Func<T1,TResult>(T1) ?

like image 447
xport Avatar asked Feb 07 '11 06:02

xport


2 Answers

It is used:

  • when coding an invoke, to provide sensible intellisense and to help the developer know which of 4 strings to pass in which position to a method(string,string,string,string) (for example)
  • in other tooling - for example implementing an event (or other delegate) and pressing tab to have the tooling generate a method stub (the parameter names from the delegate become the parameter names of the method-stub)

In both cases this name adds meaning that aids the developer. For example, I have no idea what your x represents - but name it something better and I'll have a clue.

As an alternative, use Action<int> and forget about it.

like image 117
Marc Gravell Avatar answered Nov 02 '22 06:11

Marc Gravell


Probably the same reason as you are forced to specify the name of a method parameter. For one thing it helps to document the purpose of the parameter.

Which is more readable?

public delegate void EventHandler(object source, EventArgs e);

public delegate void EventHandler(object, EventArgs);
like image 27
Joe Avatar answered Nov 02 '22 06:11

Joe