public void EventAddRemoveAnonymousFunction() { var x = new EventThing(); x.MyEvent += (object sender, EventArgs args) => { HandlerToRemove(sender, args); }; x.MyEvent -= (object sender, EventArgs args) => { HandlerToRemove(sender, args); }; x.OnMyEvent(); }
public void EventAddRemoveMethod() { var x = new EventThing(); x.MyEvent += HandlerToRemove; x.MyEvent -= HandlerToRemove; x.OnMyEvent(); }
public void EventAddRemoveLambda() { var x = new EventThing(); x.MyEvent += (sender, args) => HandlerToRemove(sender, args); x.MyEvent -= (sender, args) => HandlerToRemove(sender, args); x.OnMyEvent(); }
public void EventAddRemoveMethodRef() { var x = new EventThing(); EventHandler handlerToRemove = HandlerToRemove; x.MyEvent += handlerToRemove; x.MyEvent -= handlerToRemove; x.OnMyEvent(); }
public void EventAddRemoveLocalFunction() { var x = new EventThing(); void handler(object sender, EventArgs args) { HandlerToRemove(sender, args); } x.MyEvent += handler; x.MyEvent -= handler; x.OnMyEvent(); }
public void TestEventNotNull() { EventThing thing = new EventThing(); DoStuff(thing); Assert.AreEqual(EventSubscriber.Count, 1); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(EventSubscriber.Count, 1, "Event sub list should still hold a subscriber reference"); thing.OnMyEvent(); }
public void TestEventNull() { EventThing thing = new EventThing(); DoStuff(thing); Assert.AreEqual(EventSubscriber.Count, 1); thing.SetEventNull(); // Should clear out the list, removing all refs to subscribers GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(EventSubscriber.Count, 0, "Setting event to null should have cleared sub list"); thing.OnMyEvent(); }
public void NoObjectInitializersWithEvents() { bool firstHappened = false; bool secondHappened = false; var x = new EventThing(); // With Events, we required two statements to initialize x.MyEvent += (sender, args) => { firstHappened = true; }; // And still do multiple handlers, like this: x.MyEvent += (sender, args) => { secondHappened = true; }; x.OnMyEvent(); Assert.True(firstHappened); Assert.True(secondHappened); }
static void DoStuff(EventThing thing) { new EventSubscriber(thing); thing.OnMyEvent(); }