2

In the current version of the .NET framework, and under normal circumstances (i.e. without intentionally modifying the invocation list), are handlers for an event always invoked in the order in which they are registered? This would be consistent with the documented behavior of multicast delegates, with which events are implemented.

The accepted answer to this question says that invoking handlers in the order of their registration is an implementation detail that may change in some future version of the framework. I believe such a change by Microsoft is unlikely, therefore I am confining my question to the current version of the .NET framework. A comment on that same answer says that it is possible to register handlers such that they are not invoked in their registration order. If this is true then please demonstrate code that results in this out-of-order execution. Please do not include code which intentionally modifies the invocation list. What I am after here is whether or not I can depend on event handler invocation occurring in same order as registration in all current versions of the .NET framework.

Community
  • 1
  • 1
HappyNomad
  • 4,458
  • 4
  • 36
  • 55
  • 1
    You *shouldn't* be writing programs that rely on this behavior. That you need to *care* is a *big* red flag. – Servy Jul 15 '13 at 15:08
  • You must not have heard of the Xbox One(Eighty) if you think Microsoft is incapable of radical changes – Sayse Jul 15 '13 at 15:09
  • @HappyNomad - I do some programming for games so I keep an eye on them, put simple, Microsoft at first basically said you would only be able to play a game you have bought on one console and then changed their mind completely. – Sayse Jul 15 '13 at 15:30
  • I never got an Xbox for Xmas, so I'm bitter and don't care about them. But at least that doesn't send up a *big* red flag, right? – HappyNomad Jul 15 '13 at 15:35
  • I've posted a [followup question](http://stackoverflow.com/questions/17659253/documenting-an-events-invocation-order). Lots of red flags awaiting you there, too. – HappyNomad Jul 15 '13 at 16:50

2 Answers2

2

You cannot be sure that an event will always be executed in a particular order. The definition of the event can always do whatever it wants, and the implementation of that event is not a part of the public API.

By default, events will use a single multicast delegate as the backing store for an event, but it is straightforward enough to use your own implementation instead. There is no way to tell (beyond looking at the source code) whether or not an event has a custom implementation or not.

One way of implementing an event to not have the described order would be:

public class Foo
{
    private Stack<Action> stack = new Stack<Action>();

    public event Action MyEvent
    {
        add
        {
            stack.Push(value);
        }
        remove { throw new NotImplementedException(); }
    }

    internal void OnMyEvent()
    {
        foreach (var action in stack)
            action();
    }
}

While most of the events in framework classes won't use a definition like this; most will use a multicast delegate, the only way to know is to look at the source code; you can't tell from, for example, looking at the documentation, whether an event is implemented like this or like:

public class Foo2
{
    public event Action MyEvent;
}
Servy
  • 202,030
  • 26
  • 332
  • 449
0

That depends on how the event is implemented.

Ordinary (field-like) events store all of their handlers in a single multicast delegate.
Multicast delegates invoke their handlers in insertion order.

Other events are free to store their handlers in some other order. However, most non-standard implementations still use multicast delegates under the covers, stored in various ways (eg, EventHandlerList)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The number of current .NET framework versions is not open ended, so I would like a more specific answer. – HappyNomad Jul 15 '13 at 15:09
  • 2
    @HappyNomad You'll need to define a specific event in a specific class to get a defined answer. – Servy Jul 15 '13 at 15:12
  • What do you mean? Events are defined using the `event` keyword in C#. Do not handler registrations on all such events work the same? – HappyNomad Jul 15 '13 at 15:14
  • @HappyNomad: No. http://stackoverflow.com/a/3010356/34397 http://msdn.microsoft.com/en-us/library/bb882534.aspx – SLaks Jul 15 '13 at 15:18