EventHandler type with no event args

Use Actions (below answer copied from https://stackoverflow.com/a/1689341/1288473):

Declaring:

public event Action EventWithoutParams; 
public event Action<int> EventWithIntParam;

Calling:

EventWithoutParams?.Invoke(); 
EventWithIntParam?.Invoke(123);

I really would advise you to use the standard EventHandler pattern here and just pass EventArgs.Empty. However, you can use Action as an event type if you really want – it is just unusual.


You have several options:

  1. Use a normal event with an EventHandler and the basic EventArg class – sure the event is empty, but does this do any harm?
  2. Make your own delegate and use this with event MyDelegateWithoutParams MyEvent;
  3. Use the Observer Pattern with IObservable instead.
  4. Let clients pass an Action to you and call this action.

I hope one of these options is to your liking. I use 1 and 4 for this kind of situation (4 mostly, if there will be only one "listener").

PS: I guess 2 won't conform to the .NET framework guidelines, so maybe that one is not the best idea ;)


if you use plain delegates surely you can do what you want but if you use events I think the best is to stick on the standard and always have object sender and EventArgs e.

if you really do not know what to pass on firing those events from your own code, just pass EventArgs.Empty as second parameter.

Tags:

C#

Events

Signals