コード例 #1
0
        public static void Main(string[] args)
        {
            WorkerIsWorking       += (s, e) => Console.WriteLine($"Working for {e.WorkingHours} hours..\n");
            WorkerIsWorking       += WantMessage;
            WorkerFinishedWorking += WorkingFinishMessage;
            DoWork(5, "No complaint!");
            unchecked
            {
            }
            Action <int, int> add = (x, y) => Console.WriteLine(x + y);
            var processor         = new ProcessData();

            processor.Process(5, 3, add);


            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //SimpleDelegate.Call();

            //MultiCastDelegates.Call();

            //ReturnValuesFromDelegate.Call();

            #region Events
            //var worker = new WorkerWithAdvance();
            //worker.WorkPerformed += new EventHandler<WorkPerformedEventArgs>(worker.Worker_WorkPerformed);
            //worker.WorkCompleted += new EventHandler(worker.Worker_WorkCompleted);
            //worker.DoWork(2, WorkType.GenerateReports);
            #endregion

            #region Delegate Inference
            //var worker = new WorkerWithAdvance();
            //worker.WorkPerformed += worker.Worker_WorkPerformed;
            //worker.WorkCompleted += worker.Worker_WorkCompleted;

            //worker.WorkCompleted -= worker.Worker_WorkCompleted;
            //worker.DoWork(2, WorkType.GenerateReports);
            #endregion

            #region Anonymous method
            //var worker = new WorkerWithAdvance();
            //worker.WorkPerformed += delegate (object sender, WorkPerformedEventArgs e)
            //{
            //    Console.WriteLine(e.Hours.ToString());
            //};
            //worker.DoWork(2, WorkType.GenerateReports);
            #endregion

            #region Lambda function
            //var worker = new WorkerWithAdvance();
            //worker.WorkPerformed += (sender, e) => Console.WriteLine(e.Hours.ToString());

            //worker.DoWork(2, WorkType.GenerateReports);
            #endregion

            #region Different use of delegate
            //var processData = new ProcessData();
            //BizRuleDelegate add = (x, y) => x + y;
            //BizRuleDelegate multiply = (x, y) => x * y;

            //processData.Process(2, 3, add);

            //processData.Process(2, 3, multiply);
            #endregion

            #region Action Example
            //var processData = new ProcessData();
            //Action<int, int> addAction = (x, y) => Console.WriteLine(x + y); ;
            //Action<int, int> multiplyAction = (x, y) => Console.WriteLine(x * y);

            //processData.ProcessAction(2, 3, addAction);
            //processData.ProcessAction(2, 3, multiplyAction);
            #endregion

            #region Func Delegate Use
            var processData = new ProcessData();
            Func <int, int, int> addFunc      = (x, y) => x + y;
            Func <int, int, int> multiplyFunc = (x, y) => x * y;

            processData.ProcessFunc(2, 3, addFunc);

            processData.ProcessFunc(2, 3, multiplyFunc);
            #endregion

            Console.ReadKey();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var customers = new List <Customer> {
                new Customer {
                    ID = 1, LastName = "lastclient1", FirstName = "firstclient1", City = "cityclient1"
                },
                new Customer {
                    ID = 2, LastName = "lastclient2", FirstName = "firstclient2", City = "cityclient1"
                },
                new Customer {
                    ID = 3, LastName = "lastclient3", FirstName = "firstclient3", City = "cityclient2"
                },
                new Customer {
                    ID = 4, LastName = "lastclient4", FirstName = "firstclient4", City = "cityclient3"
                }
            };

            var cityCust = customers.Where(c => c.City == "cityclient1" && c.ID < 2).OrderBy(c => c.FirstName);

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

            Console.WriteLine("************************************************************");

            BizRuleDelegate addDel = (x, y) => x + y;

            var process = new ProcessData();

            process.Process(3, 8, addDel);
            Action <int, int> myAction = (x, y) => Console.WriteLine("Action lambda in: " + (x + y));

            process.ProcessAction(2, 3, myAction);

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

            process.ProcessFunc(2, 4, funcAddDel);

            Console.WriteLine("************************************************************");

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

            del1(4, WorkType.Golf);
            del2(10, WorkType.GeneralReports);
            DoWork(del1);

            del1 += del2 + del3;
            int finalHours = del1(4, WorkType.Golf);// last value returned

            Console.WriteLine(finalHours);

            Console.WriteLine("************************************************************");

            var worker = new Worker();

            worker.WorkPerformed  += new WorkPerformedHandler(WorkPerformed1);
            worker.WorkPerformed2 += (s, e) => Console.WriteLine("1 line lambda worked: " + e.Hours + " " + e.WorkType);
            worker.WorkPerformed2 += new WorkPerformedHandler2(Worker_WorkPerformed);
            worker.WorkPerformed3 += new EventHandler <WorkPerformedEventArgs>(Worker_WorkPerformed);
            worker.WorkPerformed3 += autogenerated_Worker_WorkPerformed;
            worker.WorkPerformed3 += delegate(object sender, WorkPerformedEventArgs e)
            {
                Console.WriteLine("Anonymouse worked: " + e.Hours + " " + e.WorkType);
            };

            worker.WorkCompleted  += new WorkCompletedHandler(Worker_WorkComleted);
            worker.WorkCompleted2 += (s, e) =>
            {
                Console.WriteLine("1) two line lambda");
                Console.WriteLine("2) Worker_WorkComleted");
            };
            worker.WorkCompleted2 += new EventHandler(Worker_WorkComleted);
            worker.WorkCompleted2 += autogenerated_Worker_WorkComleted;

            worker.DoWork(8, WorkType.GeneralReports);
        }