When forming an event, it is convenient to use the delegate Action<T1,T2> When subscribing to an event, a function is formed with the signature specified by the delegate, but the names of the variables are set automatically and not very convenient. Is it possible to specify the names of variables when describing the Action<T1,T2> delegate?

Ideally, I wanted something like

 [Attribute("index", "countDevice", "temperature")] public event Action<ushort, ushort, float> MyEvent; 

thus the code will be clear for use

  • What variables mean? add sample code - Grundy
  • @Grundy, apparently, means the names of the parameters of the method. - Vlad
  • one
    And also, for events it is better to use the EventHandler {T} and EventHandler types. - Vlad
  • 2
    @PavelMayorov met the opposite point of view in the guides and I think that it is true. so in event, I stick to creating arguments. After adding the parameters in the arguments, the old code will work. With Action, you will have to add a parameter and repair old code everywhere. - vitidev
  • one
    @PavelMayorov, I stick more to the point of view of guidelines. There is no point in using Action for events. EventHandler and EventHandler {T} are quite suitable for all occasions. And even worse, in my opinion, invent their delegates out of the blue. - Vlad

2 answers 2

If you need to set delegate parameter names, the easiest way is to create your own delegate type. You can right next to the event itself:

 public delegate void FooHandler(Bar bar, Baz baz); public event FooHandler Foo; 

If the task allows the same parameter names for different events - you can make your own generic delegate:

 public delegate void GenericEventHandler<T1, T2> (T1 sender, T2 e); // ... public event GenericEventHandler<Bar, Baz> Foo; 

You can also search for prepared delegates with the appropriate parameter names. For example, there is such a rarely used delegate EventHandler<TEventArgs>

  • I understand that you can explicitly describe the delegate, but each time to come up with names for delegates who are in principle not needed, a waste of time. I thought maybe through attributes like something to describe the names of variables. - Roman
  • The delegate name is the name of the event + Handler or EventHandler . You do not need to invent anything, you can create a snippet to create an event and a delegate to it right away. - Pavel Mayorov
  • And here Event Handler? I need my signature. - Roman
  • I'm talking about choosing a name for the delegate. What is not satisfied with the way "to take the name of the event and add to the end Handler"? - Pavel Mayorov
  • It is possible, but I wanted to avoid it. The answer is, in principle, received, thank you all. - Roman

Directly describe the names can not be.
But you can mitigate the problem using xml comments:

 /// <summary> /// <para>Имя и описание первого аргумента.</para> /// <para>Имя и описание второго аргумента.</para> /// <para>Имя и описание третьего аргумента.</para> /// </summary> public event Action<ushort, ushort, float> MyEvent; 

A hint will be displayed on MyEvent.