Last active
December 19, 2015 03:19
-
-
Save zeroeth/5889615 to your computer and use it in GitHub Desktop.
C# Delegate Addition.
This file contains hidden or 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
| // 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