I'm developing a game in Unity (C#) and I'm relying on C# events to update the game state (i.e.: check whether the game is completed after a given action).
Consider the following scenario:
// model class
class A
{
// Event definition
public event EventHandler<EventArgs> LetterSet;
protected virtual void OnLetterSet (EventArgs e)
{
var handler = LetterSet;
if (handler != null)
handler (this, e);
}
// Method (setter) that triggers the event.
char _letter;
public char Letter {
get { return _letter }
set {
_letter = value;
OnLetterSet (EventArgs.Empty);
}
}
}
// controller class
class B
{
B ()
{
// instance of the model class
a = new A();
a.LetterSet += HandleLetterSet;
}
// Method that handles external (UI) events and forward them to the model class.
public void SetLetter (char letter)
{
Debug.Log ("A");
a.Letter = letter;
Debug.Log ("C");
}
void HandleCellLetterSet (object sender, EventArgs e)
{
// check whether the constraints were met and the game is completed...
Debug.Log ("B");
}
}
Is it guaranteed that the output will always be A B C
? Or could the event subscriber's execution (HandleCellLetterSet ()
) be delayed until the next frame resulting in A C B
?
EDIT: B
is the only subscriber of A.LetterSet
. The question is not about execution order between multiple subscribers.