Example #1
0
        static void Main(string[] args)
        {
            // lambda intergrate with delegate make code consize
            BizRuleDelegate addDel   = (x, y) => x + y;
            BizRuleDelegate multiDel = (x, y) => x * y;
            var             data     = new ProcessData();

            data.Process(2, 3, addDel);
            data.Process(2, 3, multiDel);

            var worker = new Worker();

            //worker.WorkPerformed += Worker_WorkPerformed;
            // assign line 26 worker_workPerformed(obj,evt )to here
            worker.WorkPerformed += delegate(object sender, WorkPerformedEventArgs e)
            {
                Console.WriteLine("Worked: " + e.Hours + " hour(s) doing: " + e.WorkType);
            };

            // worker.WorkCompleted += Worker_WorkCompleted;
            //  worker.WorkCompleted -= Worker_WorkCompleted;
            worker.WorkCompleted += delegate(object sender, EventArgs e)
            {
                Console.WriteLine("Work is complete!");
            };
            worker.DoWork(8, WorkType.GenerateReports);

            Console.Read();
        }
        static void Main(string[] args)
        {
            var custs = new List <Customer>
            {
                new Customer {
                    City = "Phoenix", FirstName = "John", LastName = "Doe", ID = 1
                },
                new Customer {
                    City = "Phoenix", FirstName = "Jane", LastName = "Doe", ID = 500
                },
                new Customer {
                    City = "Seattle", FirstName = "Suki", LastName = "Pizzoro", ID = 3
                },
                new Customer {
                    City = "New York City", FirstName = "Michelle", LastName = "Smith", ID = 4
                },
            };

            var phxCusts = custs
                           .Where(c => c.City == "Phoenix" && c.ID < 500)
                           .OrderBy(c => c.FirstName);

            foreach (var cust in phxCusts)
            {
                Console.WriteLine(cust.FirstName);
            }

            var data = new ProcessData();
            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multiplyDel = (x, y) => x * y;

            data.Process(2, 3, multiplyDel);

            Action <int, int> myAction         = (x, y) => Console.WriteLine(x + y);
            Action <int, int> myMultiplyAction = (x, y) => Console.WriteLine(x * y);

            data.ProcessAction(2, 3, myMultiplyAction);

            Func <int, int, int> funcAddDel      = (x, y) => x + y;
            Func <int, int, int> funcMultipleDel = (x, y) => x * y;

            data.ProcessFunc(3, 2, funcMultipleDel);

            var worker = new Worker();

            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Worked: " + e.Hours + " hour(s) doing: " + e.WorkType);
                Console.WriteLine("Some other value");
            };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Work is complete!");
            worker.DoWork(8, WorkType.GenerateReports);

            Console.Read();
        }
Example #3
0
        static void Main(string[] args)
        {
            var custs = new List <Customer>
            {
                new Customer {
                    City = "Hyderabad", FirstName = "Poojitha", LastName = "Pancheti", ID = 1
                },
                new Customer {
                    City = "Hyderabad", FirstName = "Srujana", LastName = "jami", ID = 2
                },
                new Customer {
                    City = "Banglore", FirstName = "Yamini", LastName = "Pancheti", ID = 3
                },
            };
            var hydCusts = custs
                           .Where(c => c.City == "Hyderabad" && c.ID < 300)
                           .OrderBy(c => c.FirstName);

            foreach (var cust in hydCusts)
            {
                Console.WriteLine(cust.FirstName);
            }

            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multiplyDel = (x, y) => x * y;
            var data = new ProcessData();

            // data.Process(2, 3, addDel);
            data.Process(3, 4, multiplyDel);
            Func <int, int, int> funcAddDel      = (x, y) => x + y;
            Func <int, int, int> funcMultipleDel = (x, y) => x * y;

            //data.ProcessFunc(3, 2, funcAddDel);
            data.ProcessFunc(4, 5, funcMultipleDel);


            Action <int, int> myAction         = (x, y) => Console.WriteLine(x + y);
            Action <int, int> myMultiplyAction = (x, y) => Console.WriteLine(x * y);

            data.ProcessAction(3, 4, myAction);

            var worker = new Worker();

            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Worked: " + e.Hours + "hours doing:" + e.WorkType);
                Console.WriteLine("Some other value");
            };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Work is complete!");
            worker.DoWork(8, WorkType.GenerateReports);
            Console.Read();
        }
Example #4
0
        static void Main(string[] args)
        {
            var customers = new List <Customer>
            {
                new Customer {
                    City = "Phoenix", FirstName = "John", LastName = "Doe", ID = 1
                },
                new Customer {
                    City = "Phoenix", FirstName = "Jane", LastName = "Doe", ID = 500
                },
                new Customer {
                    City = "Seattle", FirstName = "Suki", LastName = "Pizzoro", ID = 3
                },
                new Customer {
                    City = "Phoenix", FirstName = "Abriel", LastName = "Lindsy", ID = 45
                },
                new Customer {
                    City = "NYC", FirstName = "Michelle", LastName = "Smith", ID = 4
                }
            };

            var phxCustomers = customers
                               .Where(c => c.City == "Phoenix" && c.ID < 500)
                               .OrderBy(c => c.FirstName); // using System.Linq to query objects

            foreach (var customer in phxCustomers)
            {
                Console.WriteLine(customer.FirstName + " is from " + customer.City);
            }

            var data = new ProcessData();
            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multiplyDel = (x, y) => x * y;

            data.Process(2, 3, multiplyDel);

            Func <int, int, int> funcAddDel      = (x, y) => x + y;
            Func <int, int, int> funcMultiplyDel = (x, y) => x * y;

            data.ProcessFunc(4, 5, funcAddDel);

            Action <int, int> addAction      = (x, y) => Console.WriteLine(x + y);
            Action <int, int> multiplyAction = (x, y) => Console.WriteLine(x * y);

            data.ProcessAction(2, 3, addAction);
            data.ProcessAction(2, 3, multiplyAction);

            var worker = new Worker();

            worker.DoWork(7, WorkType.GenerateReports);

            /*
             * // Delegate Inference:
             * worker.WorkPerformed += worker_WorkPerformed;
             * worker.WorkCompleted += worker_WorkCompleted;
             *
             * worker.DoWork(8, WorkType.GenerateReports);
             *
             * static void worker_WorkPerformed(object sender, WorkPerformedEventArgs e)
             * {
             *  Console.WriteLine("Hours worked: " + e.Hours + " " + e.WorkType);
             * }
             * static void worker_WorkCompleted(object sender, EventArgs e)
             * {
             *  Console.WriteLine("Work is done");
             * }
             */

            // Lambda Approach for the code above:
            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Hours worked: " + e.Hours + " " + e.WorkType);
            };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Work is done");

            Console.Read();
        }