C# Events without arguments. How do I handle them?

前端 未结 5 1315
无人及你
无人及你 2020-12-30 23:35

I\'m currently working on a menu system for a game and I have the standard hierarchy for screens and screen elements. (Screens contain some collection of screen elements). I

5条回答
  •  隐瞒了意图╮
    2020-12-31 00:23

    You can use Action delegates. This is much more elegantly then using data you will never need (I mean EventArgs).

    Here you define events:

    public event Action EventWithoutParams;
    public event Action EventWithIntParam;
    

    And here you fire events:

    EventWithoutParams();
    EventWithIntParam(123);
    

    You can find all the information you need at Action or Action.

    Either of these events can be initialised with a no-op delegate ... = delegate { }; so that you don't need to check for null before firing the event.

提交回复
热议问题