EventHandler type with no event args

末鹿安然 提交于 2019-12-01 03:18:28
Carsten

You can do a couple of things:

  1. Use your normal event with EventHandler and the basic EventArg class - sure it's empty but does this hurt?
  2. Make your own delegate and use this with event MyDelegateWithoutParams MyEvent;
  3. Use the Observer-Pattern with IObservable instead
  4. Let the clients pass a Action do you and call this action

I hope one of this 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 this is not the best idea ;)

I really would advise you to use the standard EventHandler patter here and just pass EventArgs.Empty; however, you can use Action as an event type of you really want - it is just unusual.

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 what to pass on firing those events from your own code, just pass EventArgs.Empty as second parameter.

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

Declaring:

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

Calling:

if (EventWithoutParams != null) EventWithoutParams(); 
if (EventWithoutParams != null) EventWithIntParam(123);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!