コード例 #1
0
        static void Main(string[] args)
        {
            var worker = new Worker();

            worker.WorkPerformed += new EventHandler <WorkPerformedEvenArgs>(Worker_WorkPerformed1);
            worker.WorkPerformed += Worker_WorkPerformed2; // Delegate inference
            worker.WorkCompleted += (s, e) => Console.WriteLine("!!!Work Completed!!!".ToUpper());;
            worker.DoWork(5, WorkType.GenerateReports);


            BizRulesDelegate AddDel   = new BizRulesDelegate((x, y) => x + y);
            BizRulesDelegate Multiply = (x, y) => x * y;
            var data = new ProcessData();

            data.Process(2, 3, AddDel);


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

            data.ProcessAction(2, 3, MultiplyAction);


            Func <int, int, int>  funcAddDel      = (x, y) => x + y;
            Func <int, int, int>  funcMultiplyDel = (x, y) => x * y;
            Func <int, int, bool> compareNumbers  = (x, y) => x == y;
            Func <bool>           returnTrue      = () => true;

            data.ProcessFuncBool(returnTrue);
            data.ProcessFunc(4, 11, compareNumbers);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            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> funcMultipleDel = (x, y) => x * y;

            data.ProcessFunc(2, 3, funcAddDel);

            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);

            var worker = new Worker();

            worker.WorkPerformed += (s, e) => Console.WriteLine("Hours worked: " + e.Hours + " " + e.WorkType);
            worker.WorkComplete  += (s, e) => Console.WriteLine("Worker is completed");

            worker.DoWork(8, WorkType.GenerateReports);

            Console.Read();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            BizRulesDelegate addDel = (x, y) => x + y;
            BizRulesDelegate mulDel = (x, y) => x * y;

            var data = new ProcessData();

            data.Process(2, 3, addDel);

            //Action<int, int> myAction = (x, y) => Console.WriteLine(x + y);
            //Action<int, int> myMulAction = (x, y) => Console.WriteLine(x * y);
            //data.processAction(2, 3, myAction);

            Func <int, int, int> funcAdd = (x, y) => x + y;
            Func <int, int, int> funcMul = (x, y) => x * y;

            data.processFunc(2, 3, funcMul);
            Worker worker = new Worker();

            //worker.WorkPerformed += delegate (object sender, WorkPerformedEventArgs e)
            //                        {
            //                            Console.WriteLine($"Hours Worked-" + e.Hours + "WorkType-" + e.WorkType);
            //                        };

            worker.WorkPerformed += (s, e) => Console.WriteLine($"Hours Worked-" + e.Hours + "WorkType-" + e.WorkType);
            worker.WorkCompleted += worker_WorkCompleted;
            worker.DoWork(05, WorkType.GoToMeetings);
        }
コード例 #4
0
        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", FirstName = "Michelle", LastName = "Smith", ID = 4
                }
            };

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

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

            var data = new ProcessData();
            BizRulesDelegate  addDel       = (x, y) => x + y;
            BizRulesDelegate  multDel      = (x, y) => x * y;
            Action <int, int> myAction     = (x, y) => Console.WriteLine(x + y);
            Action <int, int> myMultAction = (x, y) => Console.WriteLine(x * y);
            //data.ProcessAction(2, 3, myAction);

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

            data.ProcessFunc(3, 2, funcAddDel);



            var worker = new Worker();

            worker.WorkPerformed += delegate(object sender, WorkPerformedEventArgs e)
            { Console.WriteLine($"Hours worked: {e.Hours}; Work Type: {e.WorkType}"); };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Worker is done.");
            worker.DoWork(8, Worker.WorkType.GenerateReports);

            Console.ReadLine();

            //WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            //WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            //WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            //del1 += del2 + del3;

            //int finalHours = del1(10, WorkType.GenerateReports);
            //Console.WriteLine(finalHours);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: MichalBogucki/BasicEvents
        static void Main(string[] args)
        {
            var custList     = CustomerRepsitory.Retrive();
            var phxCustomers = custList
                               .Where(c => c.City.Equals("Phoenix"))
                               .OrderByDescending(c => c.FirstName);

            foreach (var singleCust in phxCustomers)
            {
                Console.WriteLine($"{singleCust.FirstName}");
            }


            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multiplyDel = (x, y) => x * y;

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

            var data = new ProcessData();

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

            Action <int, int> myAddAction      = (x, y) => Console.WriteLine($"\n{x + y}");
            Action <int, int> myMultiplyAction = (x, y) => Console.WriteLine($"\n{x * y}");

            data.ProcessAction(2, 3, myAddAction);
            data.ProcessAction(2, 3, myMultiplyAction);

            data.ProcessFunc(3, 2, funcAddDel);
            data.ProcessFunc(3, 2, funcMultiplyDel);

            // WorkPerformedHandler delegate1 = new WorkPerformedHandler(WorkPerformed1);
            // WorkPerformedHandler delegate2 = new WorkPerformedHandler(WorkPerformed2);
            // WorkPerformedHandler delegate3 = new WorkPerformedHandler(WorkPerformed3);

            //delegateHandler1(5, WorkType.Golf);
            //delegateHandler2(10, WorkType.GenerateReports);


            //DoWork(delegateHandler1);
            //DoWork(delegateHandler2);


            //delegate1 += delegate2 + delegate3;
            //
            //delegate1(10, WorkType.GenerateReports);
            //DoWork(delegate1);

            var worker = new Worker();

            worker.WorkPerformed += Worker_WorkPerformed;
            worker.WorkCompleted += Worker_WorkCompleted;
            worker.DoWork(8, WorkType.GenerateReports);

            Console.Read();
        }
コード例 #6
0
        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, addDel);
            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(3, 2, funcAddDel);
            data.ProcessFunc(3, 2, funcMultiplyDel);

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

            data.ProcessAction(2, 3, myAction);
            data.ProcessAction(2, 3, myMultiplyAction);


            var worker = new Worker();

            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Hours Worked: " + e.Hours + " " + e.WorkType);
                //Console.WriteLine("Something else.");
            };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Worker is done");

            worker.DoWork(8, WorkType.GenerateReports);
            Console.Read();
        }
コード例 #7
0
        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
                }
            };
            // Wrap down statements to increase readability
            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();
            // Create delegates that define the action we want to be taken
            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multiplyDel = (x, y) => x * y;

            data.Process(2, 3, addDel);

            // Using Func, no defining the delegate
            Func <int, int, int> funcAddDel      = (x, y) => x + y;
            Func <int, int, int> funcMultiplyDel = (x, y) => x * y;

            data.ProcessFunc(3, 2, funcAddDel);

            var worker = new Worker();

            // Using lambda expressions
            //Using brackets allows for multiple lines of code
            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Worked: " + e.Hours + " hour(s) doing: " + e.WorkType);
                Console.WriteLine("Other value here");
            };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Worker is done");
            worker.DoWork(8, WorkType.GenerateReports);

            // Keeps the window open
            Console.Read();
        }
コード例 #8
0
        static void Main(string[] args)
        {
            var data = new ProcessData();

            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multipleDel = (x, y) => x * y;

            data.Process(2, 3, multipleDel);

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

            data.ProcessFunc(6, 6, funcMultipleDel);



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

            data.ProcessAction(2, 3, myMultipleyAction);



            //var del1 = new WorkPerfromedHandler(WorkPerformed1);
            //var del2 = new WorkPerfromedHandler(WorkPerformed2);
            //var del3 = new WorkPerfromedHandler(WorkPerformed3);

            //del1 += del2 + del3;


            //del1(5, WorkType.GoToMeetings);

            var worker = new Worker();

            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Worked " + e.Hours);
                Console.WriteLine("Some other value");
            };

            worker.WorkCompleted += (s, e) => Console.WriteLine("Worker is done");

            //worker.WorkCompleted += delegate(object sender, EventArgs e)
            //{
            //    Console.WriteLine("Worker is done");
            //};



            worker.DoWork(8, WorkType.Golf);


            Console.ReadLine();
        }
コード例 #9
0
        static void Main(string[] args)
        {
            /*  var worker = new Worker();
             * worker.WorkPerformed += (s,e) => Console.WriteLine(e.Hours + "  " + e.WorkType);
             * worker.WorkCompleted += (s,e) => Console.WriteLine("Worker is Done!");
             * worker.doWork(8, WorkType.GenerateReports);
             * Console.Read();*/
            BizRulesDelegate addDel = (x, y) => x + y;

            ProcessData.Process(2, 5, addDel);
        }
コード例 #10
0
        static void Main(string[] args)
        {
            var customers = new List <Customer>
            {
                new Customer {
                    City = "Złotokłos", FirstName = "Grzesio", LastName = "Złotokłoski", ID = 4
                },
                new Customer {
                    City = "Piaseczno", FirstName = "Jan", LastName = "Kowalski", ID = 1
                },
                new Customer {
                    City = "Złotokłos", FirstName = "Seba", LastName = "Jarząbek", ID = 2
                },
                new Customer {
                    City = "Warszawa", FirstName = "Brajan", LastName = "NULL", ID = 3
                },
            };

            var zloCustomers = customers.Where(c => c.City == "Złotokłos").OrderBy(c => c.LastName);

            foreach (var customer in zloCustomers)
            {
                Console.WriteLine(customer.LastName);
            }

            var data = new ProcessData();

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

            data.ProcessFunc(3, 6, funcMultiplyDel);

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

            data.ProcessAction(2, 3, myAction);
            data.ProcessAction(2, 3, myMultiplyAction);


            var worker = new Worker();

            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Worked: " + e.Hours + " hours, done : " + e.WorkType);
                return(e.Hours);
            };

            // Anonymous method below:
            worker.WorkCompleted += (s, e) => Console.WriteLine("Worker is done.");

            worker.DoWork(8, WorkType.GenerateReports);

            Console.Read();
        }
コード例 #11
0
        static void Main(string[] args)
        {
            // 2 Instantiation
            //WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            //WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            //WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            //del1 += del2 + del3;
            //int totalHours = del1(null, new WorkPerformedEventArgs(5, WorkType.Golf));
            ////int totalHours = del1(5, WorkType.Golf);
            //// totalHours get the value of the last invoked delegate
            //Console.WriteLine(totalHours);
            //DoWork(del2);

            // events demonstration
            var worker = new Worker();

            // using anonymous method
            //worker.WorkPerformed += delegate (object sender, WorkPerformedEventArgs e)
            //                       {
            //                           Console.WriteLine($"Hours worked: {e.Hours} {e.WorkType}");
            //                       };
            // using lambda expressions
            worker.WorkPerformed += (s, e) => Console.WriteLine($"Hours worked: {e.Hours} {e.WorkType}");
            worker.WorkCompleted += Worker_WorkCompleted;
            worker.DoWork(5, WorkType.GenerateReports);

            BizRulesDelegate  addDel           = (x, y) => x + y;
            BizRulesDelegate  multiplyDel      = (x, y) => x * y;
            Action <int, int> myActionSum      = (x, y) => Console.WriteLine(x + y);
            Action <int, int> myActionMultiply = (x, y) => Console.WriteLine(x * y);

            var data = new ProcessData();

            data.Process(3, 2, multiplyDel);
            data.ProcessAction(5, 7, myActionMultiply);

            Console.Read();
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: ips138/Bootcamp2018
        static void Main(string[] args)
        {
            //var del1 = new WorkPerformedHandler(WorkPerf1);
            //var del2 = new WorkPerformedHandler(WorkPerf2);
            //var del3 = new WorkPerformedHandler(WorkPerf3);

            //del1(5, WorkType.Golf);
            //del2(10, WorkType.GenerateReports);

            //del1 += del2;
            //del1 += del3;
            //del1(10, WorkType.GoToMeetings);

            //del1 += del2 + del3;
            //var totalH = del1(11, WorkType.GoToMeetings);
            //Console.WriteLine(totalH);
            var pd = new ProcessData();

            pd.Process(2, 3, (a, b) => a * b);

            pd.ProcessAction(2, 3, new Action <int, int>((x, y) => Console.WriteLine(x + y)));

            var result = pd.ProcessFunction(3, 2, new Func <int, int, int>((x, y) => x - y));

            Console.WriteLine("Result of function is " + result);

            Worker worker = new Worker();

            worker.WorkPerformed += new EventHandler <WorkPerformedEventArgs>(Worker_WorkPerformed);
            worker.WorkCompleted += Worker_WorkCompleted;
            worker.WorkCompleted += delegate(object sender, EventArgs a)
            {
                Console.WriteLine("Success - via anonymous method");
            };
            worker.WorkCompleted += (s, e) => Console.WriteLine("Yay - via Lambda");
            worker.DoWork(8, WorkType.GenerateReports);
        }
コード例 #13
0
        static void Main(string[] args)
        {
            //WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            //WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            //WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            //del1(5, WorkType.Golf);
            //del2(10, WorkType.GenerateReports);
            BizRulesDelegate addDel      = (x, y) => x + y;
            BizRulesDelegate multiplyDel = (x, y) => x * y;

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

            Action <int, int> myAction  = (x, y) => Console.WriteLine(x + y * 2);
            Action <int, int> mymAction = (x, y) => Console.WriteLine(x * y * 2);
            var data = new ProcessData();

            data.Process(2, 3, addDel);
            data.Process(2, 3, multiplyDel);
            data.ProcessAction(2, 3, myAction);
            data.ProcessAction(2, 3, mymAction);

            data.ProcessFunc(2, 3, funcAddDel);
            data.ProcessFunc(2, 3, funcMultiplyDel);

            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 Your City", FirstName = "Michelle", LastName = "Smith", ID = 4
                }
            };

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

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

            var worker = new Worker();

            //worker.WorkPerformed += new EventHandler<WorkPerformedEventArgs>(Work_WorkPerformed);
            //worker.WorkCompleted += new EventHandler(Work_WorkCompleted);
            // Delegate Inference
            worker.WorkPerformed += (s, e) => Console.WriteLine(e.Hours + " " + e.WorkType);
            worker.WorkCompleted += (s, e) => Console.WriteLine("Done! ");

            worker.DoWork(4, WorkType.GenerateReports);
            //worker.WorkPerformed -= Work_WorkPerformed;
            //worker.WorkCompleted -= Work_WorkCompleted;

            //del1 += del2;
            //del1 += del3;

            //del1 += del2 + del3;

            //int finalHours = del1(10, WorkType.GoToMeetings);
            //Console.WriteLine(finalHours);


            Console.ReadLine();
        }
コード例 #14
0
ファイル: Program.cs プロジェクト: BhavanaShrotri/CSharp
        static void Main(string[] args)
        {
            var custs = new List <Customer>
            {
                new Customer {
                    ID = 01, City = "Pune", FirstName = "Bhavana", LastName = "Shrotri"
                },
                new Customer {
                    ID = 02, City = "Satara", FirstName = "Rachana", LastName = "Shrotri"
                },
                new Customer {
                    ID = 03, City = "Kolhapur", FirstName = "Sagar", LastName = "Shinde"
                },
                new Customer {
                    ID = 04, City = "Pune", FirstName = "Anjali", LastName = "Naik"
                },
                new Customer {
                    ID = 05, City = "Nashik", FirstName = "Priyanka", LastName = "Desai"
                }
            };

            var PunCust = custs
                          .Where(c => c.City == "Pune")
                          .OrderBy(c => c.FirstName);

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

            //WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            //WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            //WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);


            //del1 += del2;
            //del1 += del3;
            //del1(10, WorkType.Golf);

            //del1 += del2 + del3;
            //int FinalHours=del1(30, WorkType.GenerateReports);

            //Console.WriteLine("Final Hours : " + FinalHours);



            //del1(5, WorkType.GenerateReports);
            //del2(4, WorkType.Golf);

            // DoWork(del1);

            //Anonymous Method
            //worker.workPerformed += delegate (object sender, WorkPerformedEventArgs e)
            //{
            //    Console.WriteLine("Hours Worked : " + e.Hours + " " + e.WorkType);
            //};


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

            data.Process(2, 3, addDel);

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

            data.ProcessAction(5, 5, myMulAction);


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

            data.ProcessFunc(10, 5, funcMulDel);

            var worker = new Worker();

            worker.workPerformed += (s, e) =>
            {
                Console.WriteLine("Hours Worked : " + e.Hours + " " + e.WorkType);
                Console.WriteLine("Done");
            };
            worker.workCompleted += (s, e) => Console.WriteLine("Work Completed.....");;

            //worker.workCompleted -= Work_WorkCompleted;
            worker.DoWork(8, WorkType.GenerateReports);



            Console.Read();
        }
コード例 #15
0
        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 phxCust = custs.Where(c => c.City == "Phoenix").OrderBy(c => c.FirstName);

            foreach (var phx in phxCust)
            {
                Console.WriteLine(phx.FirstName + " from " + phx.City);
            }


            //3.creating delegate variables
            //WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            //WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            //WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            //del1 += del2 + del3;

            //int finalHours = del1(10, WorkType.GenerateReports);
            //writes out only value for the last delegate
            //even tho 3 of 'em were invoked
            //Console.WriteLine(finalHours);

            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(2, 3, funcMultiplyDel);



            Action <int, int> myAddAction      = (x, y) => Console.WriteLine(x + y);
            Action <int, int> myMultiplyAction = (x, y) => Console.WriteLine(x * y);
            //data.ProcessAction(2, 5, myMultiplyAction);

            var worker = new Worker();

            worker.WorkPerformed += (s, e) => Console.WriteLine("Hours worked: " + e.Hours + " hour(s) doing " + e.WorkType); /*option 2 new EventHandler<WorkPerformedEventArgs>(Worker_WorkPerformed);*/

            worker.WorkCompleted += (s, e) => Console.WriteLine("Worker is done");                                            /*option 2 new EventHandler(Worker_WorkCompleted);*/

            worker.DoWork(8, WorkType.GenerateReports);

            Console.ReadKey();
        }
コード例 #16
0
        private 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 = "New York City", FirstName = "Michelle", LastName = "Smith", ID = 4
                }
            };

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

            foreach (var cust in pxhCustomers)
            {
                Console.WriteLine($"{cust.LastName}, {cust.FirstName} from {cust.City }");
            }

            BizRulesDeleate addDel      = (x, y) => x + y;
            BizRulesDeleate multiplyDel = (x, y) => x * y;

            var data = new ProcessData();

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

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

            data.ProcessAction(2, 3, myAction);
            data.ProcessAction(2, 3, myMultiplyAction);

            Func <int, int, int> functionAddDelegate      = (x, y) => x + y;
            Func <int, int, int> functionMultiplyDelegate = (x, y) => x * y;

            data.ProcessFunction(3, 2, functionAddDelegate);
            data.ProcessFunction(3, 2, functionMultiplyDelegate);

            var worker = new Worker();

            //            worker.WorkPerformed += new EventHandler<WorkPerformedEventArgs>(WorkPerformed);
            //            worker.WorkCompleted += new EventHandler(WorkCompleted);

            // delegate inference:
            //            worker.WorkPerformed += WorkPerformed;
            //            worker.WorkCompleted += WorkCompleted;

            // to remove the delegate from the handler:
            //            worker.WorkCompleted -= WorkCompleted;

            // anonymous methods:
            //            worker.WorkPerformed +=
            //                delegate (object o, WorkPerformedEventArgs e)
            //                {
            //                    Console.WriteLine($"Work Performed: {e.Hours} hour(s) of {e.WorkType}");
            //                };
            //            worker.WorkCompleted += delegate(object o, EventArgs e) { Console.WriteLine("Work completed"); };

            // lambdas
            worker.WorkPerformed += (o, e) =>
            {
                Console.WriteLine($"Work Performed: {e.Hours} hour(s) of {e.WorkType}");
                Console.WriteLine("This line just shows how you can have multiple lines of code in a lambda.");
            };
            worker.WorkCompleted += (o, e) => Console.WriteLine("Work completed");

            worker.Dowork(8, WorkType.GenerateReports);
        }
コード例 #17
0
        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);
            }

            Func <Customer, bool> funcSeattleFilterDel = (c) => c.City == "Seattle";
            var seattleCusts = custs.Where(funcSeattleFilterDel);

            foreach (var cust in seattleCusts)
            {
                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(2, 3, multiplyDel);

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

            data.ProcessFunc(2, 3, funcAddDel);
            data.ProcessFunc(2, 3, funcMultiplyDel);

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

            data.ProcessAction(2, 3, myAction);
            data.ProcessAction(2, 3, myMultiplyAction);

            //WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            //WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            //WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            //DoWork(del1);
            //DoWork(del2);

            //del1 += del2 + del3;
            //int finalHours = del1(10, WorkType.GenerateReports);
            //Console.WriteLine("finalHours: {0}", finalHours);

            var worker = new Worker();

            //worker.WorkPerformed += Worker_WorkPerformed;
            //worker.WorkCompleted += Worker_WorkCompleted;

            /*
             * worker.WorkPerformed += delegate (object sender, WorkPerformedEventArgs e)
             * {
             *  Console.WriteLine("Hours worked:{0}, workType:{1}", e.Hours, e.WorkType.ToString());
             * };
             *
             * worker.WorkCompleted += delegate (object sender, EventArgs e)
             * {
             *  Console.WriteLine("Worker is done");
             * };
             */

            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine("Hours worked:{0}, workType:{1}", e.Hours, e.WorkType.ToString());
            };

            worker.WorkCompleted += (s, e) =>
            {
                Console.WriteLine("Worker is done");
            };

            worker.DoWork(9, WorkType.Golf);

            Console.ReadLine();
        }