예제 #1
0
        // note we have to pass in
        public Person(string name, clockTower tower)
        {
            _name  = name;
            _tower = tower;                 // has ref to tower

            // wireup a anonymous method, method, lambda,...
            _tower.Chime += () => Console.WriteLine("{0} heard the clock chime", _name);

            // example two - have to take in two parameters
            _tower.Chime2 += (object sender, EventArgs args) => Console.WriteLine("{0} heard the clock chime", _name);

            // example three - using custom event args
            _tower.Chime3 += (object sender, ClockTowerEventArgs args) => {
                Console.WriteLine("{0} heard the clock chime", _name);
                switch (args.Time)
                {
                case 6:
                    Console.WriteLine("{0} is waking up.", _name);
                    break;

                case 17:
                    Console.WriteLine("{0} is going home.", _name);
                    break;
                }
            };
        }
예제 #2
0
        // add functionality to a class where you do not have access to the source
        private static void Example1()
        {
            var ct = new clockTower();              // ignore this, im using the person class used in another example to reduce code
            var p  = new Person("John", ct);

            p.SayHello();

            var op = new Person("Sally", ct);

            p.SayHelloToOtherPerson(op);
        }
예제 #3
0
        /// <summary>
        /// Steps:
        ///     Example 1: Create a delegate "EventHandler"
        ///                Create a instance of this event handler in the object dispatching the event ie., clock tower
        ///                Pass a reference of this instance to the objec that will listen
        ///                Use this reference to "wire up" code to respond using anonymous methods, lambda expression,...
        /// </summary>


        private static void Example1()
        {
            var tower = new clockTower();

#if DEBUG
#pragma warning disable 0219
#endif
            var john = new Person("John", tower);
#if DEBUG
#pragma warning restore 0219
#endif

            tower.ChimeFivePM();
            tower.ChimeSixAM();

            // cannot differentiate between the two chimes, see Example 2
        }