Example #1
0
        static void Main(string[] args)
        {
            MyDelegate del = new MyDelegate(display);

            del("Hello C#");

            MyDelegate del2 = display;

            del2("Chichi");
            del2.Invoke("Chima");

            /*Multicast Delegate*/
            MyDelegate dele = Display;

            dele += Show;
            dele += Screen;
            dele("C# Developer");

            dele -= Show;
            dele("dot NET");

            /*Using the GetInvocationList method to loop through the methods in a delegate*/
            IntClass    intClass = new IntClass();
            IntDelegate intDel   = intClass.Get15;

            intDel += intClass.Get20;
            intDel += intClass.Get30;

            foreach (IntDelegate method in intDel.GetInvocationList())
            {
                Console.WriteLine(method());
            }
            /*Built in delegates: Action*/
            Action action = EmptyClass.EmptyMethod;

            action += EmptyClass.VoidMethod;
            action();

            /*Built in deledates: Action<>*/
            MyClass myclass = new MyClass();
            Action <string, string> devLang = myclass.Dev;

            devLang("Tochukwu ", "C#");

            Action <string, int> devSal = myclass.DevSal;

            devSal("Tochukwu ", 33000);

            /*Built in delegates: Func<> */
            Func <string, string> myname = myclass.DevName;

            Console.WriteLine(myname("Tochukwu Nwachukwu"));

            Func <int, int, int> arithmetic = myclass.Add;

            arithmetic += myclass.Subtract;

            foreach (Func <int, int, int> method in arithmetic.GetInvocationList())
            {
                Console.WriteLine(method(10, 5));
            }

            /*Built in delegates: Predicate<T>*/
            Predicate <int> isEven = myclass.Even;

            Console.WriteLine(isEven(7));

            /*Covariance*/
            PersonDelegate personDel = new Student().StudentName;

            Console.WriteLine(personDel("Jimi"));

            /*Contravarience */
            Student student = new Student {
                Name = "Gomo Briam"
            };
            ChildDelegate childDel = student.PersonName;

            childDel(student);

            /*Problems with using delegates*/
            Room room = new Room();

            room.roomTemp    = room.Alarm;
            room.Temperature = 68;
            //Does not display as expected but contrary to the book's expectation. I don't see the probelm here.
            room.Temperature = 55;
            room.Temperature = 63;

            /*Anonymous methods*/
            Action act = delegate()
            {
                Console.WriteLine("An anonymouse method of type Action");
            };
            Func <int, int> func = delegate(int num)
            {
                Console.Write("An anonymouse method of type Func<int, int> returns: ");
                return(num);
            };

            act();
            Console.WriteLine(func(33000));

            /*Passing anonymous method as argument to a method.*/
            room.AcceptAction(delegate()
            {
                Console.WriteLine("Passing anonymouse method of type Action");
            });
            room.AcceptFunc(delegate(int num)
            {
                return(num);
            }, 28000);

            /*Anonymouse methods using Lambda Expression*/
            Action actLamb = () =>
            {
                Console.WriteLine("Anonymouse method of type Action using the Lambda expression");
            };
            Func <int, int> funcLamb = (int salary) =>
            {
                Console.Write("Anonymouse method of type Func<int int> using Lambda expresssion returns: int ");
                return(salary);
            };

            actLamb();
            Console.WriteLine(funcLamb(28000));;

            /*Single line Lambda Expression*/
            Action          singleAct  = () => Console.WriteLine("Single line Lambda expresion with no return value");
            Func <int, int> singleFunc = (int x) => x + 1000;

            singleAct();
            Console.WriteLine(singleFunc(18000));

            /*Anonymous methods without specifying parameter type.*/
            Action <string> user     = (name) => Console.WriteLine("User name is {0}", name);
            Action <string> username = name => Console.WriteLine("User is {0}", name); //You may omit the parenthesis for single parameter
            Func <int, int> userID   = (id) => id;

            user("truetochukz");
            username("tochukz");
            Console.WriteLine(userID(1054375));

            /*Pass Lambda expression as method argument*/
            room.AcceptAction(() => Console.WriteLine("Labda expression as Method argument"));
            room.AcceptFunc((salary) => salary, 21000);

            /*Events*/
            Employee tochukwu = new Employee {
                Name = "Tochukwu", Position = "senior", Salary = 32000
            };
            Employee tochi = new Employee {
                Name = "Tochi", Position = "junior", Salary = 18900
            };
            Employee chuks = new Employee {
                Name = "Chuks", Position = "senior", Salary = 28000
            };
            Company company = new Company();

            company.AddSenoir += company.OnSenior;
            company.AddSenoir += company.OnShowSalary;

            company.AddWoker(tochukwu);
            company.AddWoker(tochi);
            company.AddWoker(chuks);

            /*Using the built in EventHandler delegate*/
            HotelRoom bigRoom = new HotelRoom {
                Price = 450, Size = "big"
            };
            HotelRoom smallRoom = new HotelRoom {
                Price = 350, Size = "small"
            };
            HotelRoom pricyBigRoom = new HotelRoom {
                Price = 950, Size = "big"
            };
            HotelRoom pricyRoom = new HotelRoom {
                Price = 850, Size = "medium"
            };

            List <HotelRoom> hotelRooms = new List <HotelRoom> {
                bigRoom, smallRoom, pricyBigRoom, pricyRoom
            };
            Hotel hotel = new Hotel("Honey Moon Hotel");

            hotel.BigRoom       += hotel.OnBigSize;
            hotel.ExpensiveRoom += hotel.OnExpensive;

            foreach (HotelRoom hiredRoom in hotelRooms)
            {
                hotel.HireRoom(hiredRoom);
            }



            Console.ReadLine();
        }
Example #2
0
        public void OnBigSize(object obj, EventArgs empty)
        {
            Hotel hotel = obj as Hotel;

            Console.WriteLine("Hotel Name: {0}.", hotel.Name);
        }