I want to implement a fast way to add listener to an event, my implementation:
public class AccountManager
{
public delegate void CheckIfLoggedInListener(EventArgs e);
public event CheckIfLoggedInListener SetCheckIfLoggedInListener;
public void CheckIfLoggedIn()
{
if(SetCheckIfLoggedInListener!=null)
SetCheckIfLoggedInListener(new EventArgs("e"));
}
}
Right now, I have to set listener first, then invoke the method, which can be easily messed up if other developer doesn't pay attention:
//this will not work, because you invoke the event before subscribing
accountManager.CheckIfLoggedIn();
accountManager.SetCheckIfLoggedInListener += (e) => { Debug.Log(e.param); };
I wonder if there is a way to make the order not mandatory?