static void Main(string[] args) { OtherPublisher g = new OtherPublisher("Big Generator"); OtherSuscriber s1 = new OtherSuscriber("Suscriber1"); g.GeneratingNumberEvent += s1.MyEventHandler; OtherSuscriber s2 = new OtherSuscriber("Suscriber2"); g.GeneratingNumberEvent += s2.MyEventHandler; g.Run(); }
private static void Escenario3() { OtherPublisher g = new OtherPublisher("Big Generator"); OtherSuscriber s1 = new OtherSuscriber("Suscriber1"); g.NewNumberEvent += s1.MyEventHandler1; OtherSuscriber s2 = new OtherSuscriber("Suscriber2"); g.NewNumberEvent += s2.MyEventHandler1; g.Run(); }
static void Main(string[] args) { OtherPublisher g = new OtherPublisher("Big Generator"); OtherSuscriber s1 = new OtherSuscriber("Suscriber1"); g.NewNumber += s1.MyEventHandler1; g.NewNumberAction += s1.MyEventHandler2; //Puedo generar un evento desde fuera del objeto g.NewNumberAction(45); //Puedo eliminar las suscripciones desde cualquier lado g.NewNumberAction = null; //g.NewNumber(34); Error, no puedo manipularlo desde afuera del objeto //g.NewNumber =null; Idem //g.Run(); }
/*Este es el manejador de eventos*/ public void MyEventHandler(OtherPublisher sender, EventArgs e) { Console.WriteLine($"Soy {this.Name} Y {sender.Name} generará un número"); }