Пример #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Listing 1-87");
            Pub p = new Pub();

            p.OnChange += (sender, e) => Console.WriteLine("Subscriber 1 called");

            p.OnChange += (sender, e) =>
            {
                throw new Exception("Error");
            };

            p.OnChange += (sender, e) => Console.WriteLine("Subscriber 3 called");

            try
            {
                p.Raise();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine(ex.InnerExceptions.Count);
            }

            Console.ReadLine();
        }
Пример #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Listing 1-84");

            var p = new Pub();

            p.OnChange += (sender, e) => Console.WriteLine("Event raised by {0}, with value: {1}", sender, e.Value);

            p.Raise();

            Console.WriteLine("Listing 1-85");

            var pca = new PubWithCustomEventAccessor();
            pca.OnChange += (sender, e) => Console.WriteLine("Handler added with custom accessor. Event raised with value: {0}", e.Value);

            pca.Raise();

            Console.ReadLine();
        }
Пример #3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Listing 1-82");

            // Your code creates a new instance of Pub, subscribes to the event with two different methods and then raises the event by calling p.Raise. 
            // The Pub class is completely unaware of any subscribers. It just raises the event.

            var p = new Pub();

            p.OnChange += () => Console.WriteLine("Event raised to method 1");
            p.OnChange += () => Console.WriteLine("Event raised to method 2");

            p.Raise();

            // Although this system works, there are a couple of weaknesses. 
            // If you change the subscribe line for method 2 to the following, 
            // you would effectively remove the first subscriber by using = instead of +=:

            p.OnChange = () => Console.WriteLine("Overwrite of onchange subscriptions");

            p.Raise();

            // Another weakness is that nothing prevents outside users of the class from raising the event.
            // By just calling p.OnChange() every user of the class can raise the event to all subscribers. 
            Console.WriteLine("Raising the event from outside");

            p.OnChange();

            Console.WriteLine();
            Console.WriteLine("Listing 1-83");

            // To overcome these weaknesses, the C# language uses the special event keyword.

            p.OnChangeEvent += () => Console.WriteLine("This from an event keyword event");

            // p.OnChangeEvent(); will result in compilation error
            p.RaiseEvent();

            Console.ReadLine();
        }
Пример #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Listing 1-86");

            Pub p = new Pub();

            p.OnChange += (sender, e) => Console.WriteLine("Subscriber 1 called");

            p.OnChange += (sender, e) =>
            {
                throw new Exception("Error");
            };

            p.OnChange += (sender, e) => Console.WriteLine("Subscriber 3 called");

            p.Raise();

            // As you can see, the first subscriber is executed successfully,
            // the second one throws an exception,
            // and the third one is never called. 

            Console.ReadLine();
        }