Created
October 30, 2012 18:12
-
-
Save ProdigySim/3981973 to your computer and use it in GitHub Desktop.
example of event nulling behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
//Expected Output: | |
// | |
//Execute: (Status: null) | |
//======= | |
//Execute: (Status: not null) | |
//Callback | |
//======= | |
//Execute: (Status: null) | |
//======= | |
//Execute: (Status: null) | |
//======= | |
//Execute: (Status: not null) | |
//Callback | |
//======= | |
//Execute: (Status: null) | |
//======= | |
namespace TestEventNulling | |
{ | |
class EventHolder | |
{ | |
public void Execute() | |
{ | |
if (MyEvent != null) | |
{ | |
MyEvent(); | |
} | |
} | |
public void NullOut() | |
{ | |
MyEvent = null; | |
} | |
public bool EventStatus() | |
{ | |
return MyEvent == null; | |
} | |
public event Action MyEvent; | |
} | |
class Program | |
{ | |
private static EventHolder m_eh; | |
static void Main(string[] args) | |
{ | |
m_eh = new EventHolder(); | |
Test(); | |
m_eh.MyEvent += Callback; | |
Test(); | |
m_eh.NullOut(); | |
Test(); | |
m_eh.MyEvent -= Callback; | |
Test(); | |
m_eh.MyEvent += Callback; | |
Test(); | |
m_eh.MyEvent -= Callback; | |
Test(); | |
} | |
static void Callback() | |
{ | |
Console.WriteLine("Callback"); | |
} | |
static void Test() | |
{ | |
Console.WriteLine("Execute: (Status: {0}null)", m_eh.EventStatus() ? "" : "not "); | |
m_eh.Execute(); | |
Console.WriteLine("======="); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment