- Home /
Why doesn't unregistering an anonymous delegate trigger an error?
If you test it, you will see that it doesn't do anything. Why is it even permitted?
using UnityEngine;
public class LogItInAwake : MonoBehaviour {
delegate void EventHandler (); EventHandler LogIt;
void Awake () { LogIt += () => Debug.Log("It"); LogIt -= () => Debug.Log("It"); LogIt(); }
}
Answer by Herman-Tulleken · Sep 23, 2010 at 09:35 AM
You can unregister any delegate without getting an error, whether it is named or not or registered or not. (Why, I don't know, possibly for efficiency reasons or simply because it is not required by the C# standard).
By holding a reference to the delegate, you can successfully unregister it:
using UnityEngine;
public class LogItInAwake : MonoBehaviour {
delegate void EventHandler(); EventHandler LogIt;
void Awake()
{
EventHandler x = () => Debug.Log("It");
LogIt += x;
LogIt -= x;
LogIt();
}
}
Some more info:
That's good info, and I didn't know that, so thank you, but as you said, you don't have an answer to the question.