Skip to content

Instantly share code, notes, and snippets.

@ProdigySim
Created October 30, 2012 18:12
Show Gist options
  • Save ProdigySim/3981973 to your computer and use it in GitHub Desktop.
Save ProdigySim/3981973 to your computer and use it in GitHub Desktop.
example of event nulling behavior
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