What does subscribing an event to another event in C# do?

Viewed 72

I recently stumbled again on an issue regarding events and I couldn't find anything that answers what this syntax actually does: event1 += event2

Here are 2 example methods with the same events for testing purposes:

private event Action A = null;
private event Action B = null;
private event Action C = null;

This version (simplified) does work (which I used, with each event having its own Trigger-method):

private void DoTest1 ()
{
    A += () => DebugText ($"A Test");
    A += () => B.Invoke ();
    B += () => DebugText ($"B Test");
    B += () => C.Invoke ();
    C += () => DebugText ($"C Test");

    A.Invoke ();
    // Result: A Test, B Test, C Test
}

This does not work as expected:

private void DoTest2 ()
{
    A += () => DebugText ($"A Test");
    A += B;
    B += () => DebugText ($"B Test");
    B += C;
    C += () => DebugText ($"C Test");

    A.Invoke ();
    // Result: A Test
}

So I wonder, why does the compiler accept the syntax if it does not seem to work? What am I missing here?

0 Answers
Related