Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created January 5, 2013 05:22
Show Gist options
  • Select an option

  • Save gogsbread/4459923 to your computer and use it in GitHub Desktop.

Select an option

Save gogsbread/4459923 to your computer and use it in GitHub Desktop.
Practical Event Delegate examples
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