1

I detected the feature of C# that it is possible to use an Action or Func like an event. What I mean is, that I can do following:

Action aAction;

aAction = DoSomething;
aAction += DoAnotherting;

// execute the action -> both functions will be executed
aAction();

aAction -= DoSomething;  // unsubscribe on function

I was not aware of this, thought using += is only possible for events. In the first moment this looks quite well, as I do not have to use the event keyword and I can also call this action from outside of the owner class (what is not possible for events). But I'm wondering, are there any good examples for such a use or is it only bad practice?

A complete example is shown here:

[TestMethod]
public void DummyTest()
{
    DummyClass myInstance = new DummyClass();

    int i = 0;

    Action action1 = () => i++;
    Action action2 = () => i += 2;

    Func<int> func1 = () => 5;

    myInstance.MyFunc += () => 3;
    myInstance.MyFunc += func1;

    Assert.AreEqual(5, myInstance.MyFunc?.Invoke() );

    myInstance.MyFunc -= func1;

    Assert.AreEqual(3, myInstance.MyFunc?.Invoke() );

    myInstance.MyAction = action1;
    myInstance.MyAction += action2;

    myInstance.MyAction?.Invoke();

    Assert.AreEqual(3, i);
    myInstance.MyAction -= action1;

    myInstance.MyAction?.Invoke();

    Assert.AreEqual(5, i);

    myInstance.MyAction = () => i = 0;

    myInstance.MyAction?.Invoke();

    Assert.AreEqual(0, i);
}


class DummyClass
{
    public Action MyAction;
    public Func<int> MyFunc;
}
user2959547
  • 95
  • 1
  • 8
  • Not really. Look here: [Actions](http://stackoverflow.com/questions/7408744/is-it-bad-practice-to-use-action-and-func-all-the-time-instead-of-making-corresp) – Maciej S. Apr 20 '16 at 14:11
  • I'd like to reference the NodaTime formatters. I've extracted an excerpt here: https://gist.github.com/Feanathiel/38fbcace35bc3f4f2d48 (based on https://github.com/nodatime/nodatime/blob/69a2cdad9cb4c32a82620eae4f2460ff9479570a/src/NodaTime/Text/Patterns/SteppedPatternBuilder.cs) – Caramiriel Apr 20 '16 at 14:15
  • This question may be better suited to http://programmers.stackexchange.com/ – Alex Apr 20 '16 at 14:27

1 Answers1

1

It is my impression that the whole point of events is to put the control of events into the enclosing type. It is not up to clients to choose when the event is fired. An event is a (collection of) function(s) that is(/are) invoked when some state is changed in a type or when something interesting happens that clients might want to react to, but the exact details should be kept hidden for the same reason that you shouldn't expose fields to clients either.

There's nothing inherently HORRIBLE with it in the sense that it's going to blow up your house, but on the other hand there is no REASON to use them like this. Events are in the language for a reason, they have semantic meaning. If you use Action/Func delegates instead of events, people who read your code will have to figure out what the heck you're doing and why you aren't using the conventional tools instead. It's just clutter/noise, so my advise is to avoid it.

sara
  • 3,521
  • 14
  • 34
  • Thanks for your comment, same as I am thinking about this issue. The reason for my question is, that I visited a software training and the trainer used actions in this way. And I was wondering, what is the advantage of this. Unfortunatelly the trainer was not able to answer my question ... – user2959547 Apr 20 '16 at 14:19
  • Yeah... and we should not use our own complexed classes/object... because it can be so confusing to others... – Maciej S. Apr 20 '16 at 14:21