Пример #1
0
        static void Main()
        {
            //delegate instance
            //WorkPerformedHandler delegate1 =
            //    new WorkPerformedHandler(WorkPerformed1);

            //WorkPerformedHandler delegate2 =
            //   new WorkPerformedHandler(WorkPerformed2);

            //WorkPerformedHandler delegate3 =
            //   new WorkPerformedHandler(WorkPerformed3);

            //delegate1 += delegate2 + delegate3;
            //int hours = delegate1(10, WorkType.Player);
            //Console.WriteLine(hours);
            //DoWork(delegate3);

            BizRulesDelegate addDelegate      = (a, b) => a + b;
            BizRulesDelegate multiplyDelegate = (a, b) => a * b;

            Func <int, int, int> funcAddDel = (a, b) => a + b;

            var data = new ProcessData();

            data.Process(2, 3, addDelegate);
            data.ProcessFunc(2, 3, funcAddDel);

            var worker = new Worker();

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

            //using lambda
            worker.WorkPerformed += (s, e) =>
            {
                Console.WriteLine(e.Hours + " " + e.WorkType);
            };

            //delegate inference
            worker.WorkPerformed += worker_WorkPerformed;

            //anonymous methods


            worker.DoWork(8, WorkType.GenerateReports);

            Action <string> messageTarget = ShowWindowsMessage;

            messageTarget("Invoking message!");

            Func <string, bool> logFunc = LogToEventLog;

            logFunc("Log Message");
        }