Created
January 5, 2013 05:22
-
-
Save gogsbread/4459923 to your computer and use it in GitHub Desktop.
Practical Event Delegate examples
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
| using System; | |
| namespace RandomMusings | |
| { | |
| public class Hospital | |
| { | |
| public event AlarmHandler Alarm; | |
| public delegate void AlarmHandler(string message); | |
| public Hospital () | |
| { | |
| } | |
| public void Disaster() | |
| { | |
| Alarm("Attack"); | |
| } | |
| } | |
| public class Ambulance | |
| { | |
| public void Handle(string message) | |
| { | |
| switch(message) | |
| { | |
| case "Attack": | |
| Console.WriteLine("Sending Ambulance"); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| } | |
| internal sealed class RushHospital | |
| { | |
| public static void Main() | |
| { | |
| Hospital h = new Hospital(); | |
| Ambulance a = new Ambulance(); | |
| string via = "postal"; | |
| h.Alarm += (message) => {Console.WriteLine("Sending Ambulance via " + via);}; | |
| h.Disaster(); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment