Ejemplo n.º 1
0
        public static void CowMain()
        {
            /*
             *  Subscribe using Event Handler.  When you subscribe, you are not calling the method.  The method is called
             *  when the event is triggered.
             */
            Cow c1 = new Cow {
                Name = "Betsy"
            };

            c1.Moo += giggle;
            Cow c2 = new Cow {
                Name = "Georgy"
            };

            c2.Moo += giggle;
            Cow victim = new Random().Next() % 2 == 0 ? c1 : c2;

            victim.BeTippedOver();

            /*
             *  Subscribe using a Lambda expression.   Ordinarily paramters are s,e... I just made them xrx,cce to
             *  show that you can make them anything.
             */
            Cow c3 = new Cow {
                Name = "Jesse"
            };

            c3.Moo += (xrx, cce) => giggle(xrx, cce);
            Cow victum2 = c3;

            victum2.BeTippedOver();

            /*
             * Subscribe using a delegate; otherwise known as an Anonymous method
             */
            Cow c4 = new Cow {
                Name = "Arthur"
            };

            c4.Moo += delegate(object s, EventArgs e)
            {
                giggle(s, e);
            };
            Cow victum3 = c4;

            victum3 = c4;
            victum3.BeTippedOver();

            /*
             * Subscribe using Lamda Expression.  Testing EventHandler using s,e but not using s,e
             */
            Cow c5 = new Cow {
                Name = "BigFoot"
            };

            c5.Moo += (s, e) => Console.WriteLine("Big Foot gets tipped over");
            c5.BeTippedOver();
        }