//Listing 82 public static void CreateAndRaise() { Pub p = new Pub(); p.OnChange += () => Console.WriteLine("Event Raised to method 1"); p.OnChange += () => Console.WriteLine("Event Raised to method 2"); p.Raise(); }
public static void CreateAndRaiseOne() { Pub p = new Pub(); //the below code is incorrect, an event cannot be directly assigned to operator //p.OnChangeOne = () => Console.WriteLine("Event Raised to method 1"); p.OnChangeOne += () => Console.WriteLine("Event Raised to method 2"); p.RaiseOne(); }
//Listing 87 public static void CreateAndRaiseFour() { Pub p = new Pub(); p.OnChangeFour += (sender, e) => Console.WriteLine("Subscriber 1 called"); p.OnChangeFour += (sender, e) => { throw new Exception(); }; /*Console.WriteLine("Subscriber 2 called");*/ p.OnChangeFour += (sender, e) => Console.WriteLine("Subscriber 3 called"); try { p.RaiseFour();} catch(AggregateException ex) { Console.WriteLine(ex.InnerExceptions.Count); } }
//Listing 84 public static void CreateAndRaiseTwo() { Pub p = new Pub(); p.OnChangeTwo += (sender, e) => Console.WriteLine("Event raised: {0}", e.Value); p.RaiseTwo(); }