Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active December 19, 2015 03:19
Show Gist options
  • Select an option

  • Save zeroeth/5889615 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/5889615 to your computer and use it in GitHub Desktop.
C# Delegate Addition.
// read http://msdn.microsoft.com/en-us/library/vstudio/ms173172.aspx for more info
// store methods in a dictionary of event names
//
public delegate void MethodCallback();
private Dictionary<string, MethodCallback> event_listeners = new Dictionary<string, Methodcallback>();
// create key entry first
//
if (!event_listeners.ContainsKey ("my event"))
{
event_listeners.Add("my event", null);
}
// delegates can be stacked using addition
//
event_listeners["my event"] += my_first_method;
event_listeners["my event"] += my_second_method;
// this will call all added methods at once
//
event_listeners["my event"](); /* triggers first and second method */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment