public static void Call()
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);

            DoWork(del1);
        }
Example #2
0
        static void Main(string[] args)
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            del1(null, new WorkPerformedEventArgs(1, WorkType.Golf));
            del2(null, new WorkPerformedEventArgs(2, WorkType.GenerateReports));
            DoWork(del1);

            Console.WriteLine(Environment.NewLine);
            del3 += del1;
            del3 += del2;
            del3(null, new WorkPerformedEventArgs(10, WorkType.GenerateReports));

            Console.WriteLine(Environment.NewLine);
            del3 = del1 + del2;
            int finalHours = del3(null, new WorkPerformedEventArgs(10, WorkType.GenerateReports));

            Console.WriteLine("finalHours:" + finalHours.ToString());

            Machine.MachineSample();
            WorkerClassSample();
            Console.ReadKey();
        }
        public static void Main(string[] args)
        {
            WorkPerformed += CompleteWork;
            WorkPerformed(50, WorkType.GenerateReports);

            WorkPerformedHandler delagetMethod = WorkPerformed as WorkPerformedHandler;

            delagetMethod(200, WorkType.DevelopSite);

            Console.WriteLine();
            Console.WriteLine("Worker class handle event!");

            Worker worker = new Worker();

            // worker.PerformWork += Worker_PerformWork;
            worker.PerformWork += new WorkPerformHandler(Worker_PerformWork);
            worker.PerformWork += Worker_PerformWorkPartTwo;
            worker.PerformWork += delegate(object sender, WorkPerformHandlerArgs workPerformHandlerArgs)
            {
                Console.WriteLine(workPerformHandlerArgs.WorkType + " => " + workPerformHandlerArgs.Hours);
            };

            worker.PerformWork += (object sender, WorkPerformHandlerArgs WorkPerformHandlerArgs) =>
            {
                Console.WriteLine("Uisng lambda function!");
                Console.WriteLine(WorkPerformHandlerArgs.WorkType + " => " + WorkPerformHandlerArgs.Hours);
            };

            worker.DoWork(200, WorkType.DevelopSoftware);
        }
        static void Main(string[] args)
        {
            WorkPerformedHandler methodDelagete = new WorkPerformedHandler(PerformWork);

            methodDelagete += PerformSecondWork;
            WorkPerformedHandler thirdDelagate = PerformThirddWork;

            methodDelagete += thirdDelagate;
            methodDelagete(150, WorkType.Programmer);

            Console.WriteLine();
            Console.WriteLine("Another way of invocation");
            //DoWork(methodDelagete, 150, WorkType.Teacher);
            DoWork(thirdDelagate, 200, WorkType.Teacher);

            Operation add      = (int a, int b) => a + b;
            Operation multiply = (int a, int b) => a * b;
            Operation subtract = (int a, int b) => a - b;

            Console.WriteLine();
            Console.WriteLine("Operations:");
            Console.WriteLine(add(10, 5));
            Console.WriteLine(multiply(10, 5));
            Console.WriteLine(subtract(10, 5));
        }
Example #5
0
        public static void Part2_Main()
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            Console.WriteLine("Calling Delegate");
            del1(5, WorkType.Golf);
            del2(10, WorkType.GenerateReports);

            DoWork(del1);


            Console.WriteLine("\nMerget together");
            del1 += del2 + del3;
            del1(5, WorkType.Golf);

            Console.WriteLine("\nReturn something using delegate");
            int finalHours = del1(10, WorkType.GenerateReports);

            Console.WriteLine(finalHours);

            var worker = new Part2_Worker();


            Console.Read();
        }
 public CreatingDelegates()
 {
     // Invoke delegate
     del1(5, WorkType.Golf);
     // Adding to the invocation list
     del1 += del2;
 }
Example #7
0
        public void RunExample()
        {
            DelegateExample1 delegateExample1 = new DelegateExample1();

            WorkPerformed += new WorkPerformedHandler(delegateExample1.WorkDone1);
            WorkPerformed += new WorkPerformedHandler(delegateExample1.WorkDone2);
            DoWork(10);
        }
        public CreateCustomDelegate()
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);


            DoWork(del1);
            DoWork(del2);
        }
Example #9
0
        static void Main(string[] args)
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);

            del1 += del2;

            del1(5, WorkType.Golf);
        }
Example #10
0
        public Worker()
        {
            WorkPerformedHandler del1 = WorkPerformed1;

            del1 += WorkPerformed2;
            WorkPerformedHandler del2 = WorkPerformed2;

            DoWork(del1);
            del2(3, WorkType.Design);
        }
Example #11
0
        private void OnWorkPerformed(int hours, WorkType type)
        {
            WorkPerformedHandler localHandler = WorkPerformed;

            if (localHandler != null)
            {
                localHandler(hours, type);
            }
            //WorkPerformed?.Invoke(hours, type);
        }
Example #12
0
        static void Main(string[] args)
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);

            del1(1, WorkType.Analysis);
            del2(5, WorkType.Development);

            Console.ReadLine();
        }
Example #13
0
        private static void Main(string[] args)
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);
            var del3 = new WorkPerformedHandler(WorkPerformed3);

            var finalDelegate = del1 + del2 + del3;
            var finalResult   = finalDelegate(1, WorkType.Work);

            Console.WriteLine(finalResult);
        }
Example #14
0
        protected virtual void OnWorkPerformed(int hours)
        {
            WorkPerformedHandler del = Workperformed as WorkPerformedHandler;

            if (del != null)
            {
                del(hours);
            }

            WorkPerformedHandler2?.Invoke(this, EventArgs.Empty);
        }
        public static void Call()
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);

            del1 += del2;

            var finalResult = DoWork(del1);

            Console.WriteLine("Final Result - " + finalResult.ToString());
        }
Example #16
0
        static void Main(string[] args)
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            del1 += del2;
            del1 += del3;

            del1(10, WorkType.GenerateReports);
        }
Example #17
0
        static void Main(string[] args)
        {
            //Delegate instance
            WorkPerformedHandler delegate1 = new WorkPerformedHandler(WorkedPerformed1);
            WorkPerformedHandler delegate2 = new WorkPerformedHandler(WorkedPerformed2);

            //Add to the invocation list
            delegate1 += delegate2;

            //Invoking delegate instance 1 and 2
            delegate1(8, "Hard work");
        }
Example #18
0
        static void Main(string[] args)
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);
            var del3 = new WorkPerformedHandler(WorkPerformed3);

            del1 += del2 + del3;

            int finalResult = del1(1, WorkType.Work);

            Console.WriteLine(finalResult);
        }
Example #19
0
        private static void Main(string[] args)
        {
            // Pipeline that dumps data to WorkPerformed1
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);

            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);

            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed2);

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

            // Pass in deletage - del2 calls WorkPerformed2. It will invoke whatever was passed ie. WorkPerformed2
            DoWork(del2);

            // Adding handlers to Invocation list of Delegate1
            // Whenever del1 fires it will notify WorkPerformed2 and WorkPerformed3 as well
            del1 += del2;
            del1 += del3; // I could also do { del1 += del2 + del3 }

            // Events with Delegates
            // Attach handlers with delegates
            // Worker class 3 public events to wire up
            var worker = new Worker();

            // Without using Generic<T>
            worker.WorkPerformed += new WorkPerformedHandlerDelEvent(worker_WorkPerformed);

            // Wire event
            // specifying parameter signature match/pipeline
            worker.WorkPerfomedT += new EventHandler<WorkPerformedEventArgs>(worker_WorkPerformed);
            worker.WorkCompleted += worker_WorkCompleted; // Delegate Inference

            // Worker performs some task that exposes it's events. Whenever event is fired
            // subscribers (worker_WorkPerformed, worker_WorkCompleted) will be notified
            // Start work
            worker.DoWork(3, WorkType.GoToMeeting);

            // Testing class
            MyWorker w = new MyWorker();
            w.AttachListenerToMe += MyWorkerNotified;

            // Using Func with Events
            // Can't do this
            //w.AttachListenerToMe += Action<object, EventArgs> = (o, e) =>
            //    {
            //        Console.WriteLine();
            //    };

            string stringResult = w.DoWork();

            Console.Read();
        }
Example #20
0
        public AddMultipleDelegates()
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);
            var del3 = new WorkPerformedHandler(WorkPerformed3);

            // Add to invocation list, called sequentially
            del1 += del2 + del3;

            // Only the last delegate to be called will be the one to return a value,
            // as we can only have 1 return type
            var finalHours = del1(10, WorkType.GenerateReports);
        }
Example #21
0
        static void Main(string[] args)
        {
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            var del2 = new WorkPerformedHandler(WorkPerformed2);
            var del3 = new WorkPerformedHandler(WorkPerformed3);

            // multicast delegate
            del1 += del2 + del3;

            del1(10, WorkType.Testing);

            Console.ReadLine();
        }
Example #22
0
        static void Main(string[] args)
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            del1 += del2 + del3;

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

            Console.WriteLine(finalHours);
            Console.Read();
        }
        public static void Call()
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

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

            //DoWork(del1);

            //Another way to do that
            del1 += del2 + del3;

            DoWork(del1);
        }
Example #24
0
        static void Main(string[] args)
        {
            // here we can subscribe to an event
            Worker worker = new Worker();

            worker.WorkCompleted += Workcompleted; // the delegete inference compile it to that >> worker.WorkCompleted +=new EventHandler<WorkPerformedEventArgs>(Workcompleted) ;
            worker.WorkPerformed += WorkPerformed2;
            worker.WorkPerformed += (object sender, int hours) =>
            {
                Console.WriteLine($"Work performed is {hours} Hours in method anonymous");
            };


            worker.DoWork(3);


            // there are here 3 seperate invocatoin list in 3 seperate multicast delegates
            WorkPerformedHandler handler  = WorkPerformed1;
            WorkPerformedHandler handler2 = WorkPerformed2;
            WorkPerformedHandler handler3 = WorkPerformed3;

            handler += handler2 + handler3;

            handler2 += handler3 + handler2;
            handler2 += handler3;

            //handler += handler2 + handler3; handler 2 will not invoke all hanler 2 from the above as its already filled changing handler2 will not
            //effect handler

            handler3 += handler;

            Console.WriteLine("handler 1 invocatrion list\n");
            DoWork(null, handler); //invoked the methode 2 times
            Console.WriteLine("\n");
            Console.WriteLine("handler 2 invocatrion list\n");
            DoWork(null, handler2);// invokes it only one time
            Console.WriteLine("\n");
            Console.WriteLine("handler 3 invocatrion list\n");
            DoWork(null, handler3);// invokes it only one time
            //handler = WorkPerformed1;;
            handler += handler2 + handler3;
            Console.WriteLine("\n");
            Console.WriteLine("AGAIN: handler 1 invocatrion list\n");
            DoWork(null, handler); //invoked the methode 2 times
        }
        public static void Main()
        {
            //Instance:
            WorkPerformedHandler dele = new WorkPerformedHandler(WorkPerformed);

            //Invoke:
            dele(10, WorkType.Meetings);

            //Add to the invocation list:
            WorkPerformedHandler first  = new WorkPerformedHandler(WorkPerformed);
            WorkPerformedHandler second = new WorkPerformedHandler(WorkPerformedSecond);

            first = first + second;    //Можем да добавим лист от делегати, които се извикват последователно.
            first(5, WorkType.Golf);

            //Lambda expressions can be assigned to any delegate:
            AddHandler ab     = (a, b) => a + b;
            int        result = ab(1, 1); //  2
        }
Example #26
0
        public static void demo()
        {
            Console.WriteLine("FIRST USE ------- Delegator chain as a work flow");
            Console.WriteLine("Preparing delegators chain");
            Console.WriteLine("Delegators could be framework code outside our reach");
            WorkPerformedHandler del = new WorkPerformedHandler(WorkPerformedFrameWork.WorkPerformed1);

            del += new WorkPerformedHandler(WorkPerformedFrameWork.WorkPerformed2); // with this we can invoke multiple listeners

            Console.WriteLine("Passing actual data to delegator chain");
            Console.WriteLine("Please notice delegators fire sequentially");
            del(1, WorkType.GenerateReports);
            del(2, WorkType.Golf);

            Console.WriteLine("NOTICE -- When Custom handler returns data, ONLY THE LAST INVOKATION IN THE LIST WILL RETURN DATA");
            WorkPerformedWithReturnValueHandler del2 = new WorkPerformedWithReturnValueHandler(WorkPerformedFrameWork.WorkPerformed4);

            del2 += new WorkPerformedWithReturnValueHandler(WorkPerformedFrameWork.WorkPerformed5);
            del2(23, WorkType.Golf);

            Console.WriteLine("SECOND USE ------- Dynamically pass delegator as function pointer");
            Console.WriteLine("In this way we can define the actions that will take place at run time, dynamically passing different delegates");
            System.Console.WriteLine("... First subcase: Use delegate defined in a framework to perform work on a local process:");
            DoTask(WorkPerformedFrameWork.WorkPerformed2, 34, WorkType.GoToMeetings);

            System.Console.WriteLine("... Second subcase: Pass a locally defined delegate to a framework process to do work");

            Action <int, WorkType> myAction = (hours, worktype) => {
                System.Console.WriteLine("This is work done on framework dynamic process, from a locally defined function (myAction)...");
                System.Console.WriteLine($"Processing data {hours} hours of {worktype}! ... myAction done!");
            };
            Func <int, WorkType, bool> myOtherAction = (hours, worktype) => {
                System.Console.WriteLine("This is work done on framework dynamic process, from a locally defined function (myOtherAction)...");
                System.Console.WriteLine($"Processing data {hours} hours of {worktype} ... myOtherAction done!");
                return(true);
            };

            WorkPerformedFrameWork.DynamicWorkPerformed1(myAction, 12, WorkType.GoToMeetings);
            var res = WorkPerformedFrameWork.DynamicWorkPerformed1(myOtherAction, 12, WorkType.GoToMeetings);

            System.Console.WriteLine($" ---> Result from last delegate invocation {res}");
        }
Example #27
0
        static void Main(string[] args)
        {
            // Single delegate
            var del1 = new WorkPerformedHandler(WorkPerformed1);
            int hour = del1(10, WorkType.Support);

            Console.WriteLine("Hour {0}", hour);

            // Multicast delegate
            var del2 = new WorkPerformedHandler(WorkPerformed2);
            var del3 = new WorkPerformedHandler(WorkPerformed3);
            var del4 = new WorkPerformedHandler(WorkPerformed4);

            del2 += del3 + del4;
            int hours = del2(10, WorkType.Support); // Will return value only of last delegate in invocation list

            Console.WriteLine("Hours {0}", hours);

            Console.ReadLine();
        }
Example #28
0
        static void DelgateSample()
        {
            WorkPerformedHandler logger     = new WorkPerformedHandler(LogEvent);
            WorkPerformedHandler notify     = new WorkPerformedHandler(NotifyUser);
            WorkPerformedHandler downstream = new WorkPerformedHandler(InformDownstream);

            Console.WriteLine("Individual Delegates");
            logger(9, WorkType.Heavy);
            notify(9, WorkType.Heavy);
            Console.Read();

            Console.WriteLine("Method Calls");
            DoWork(logger, 2, WorkType.Light);
            DoWork(notify, 2, WorkType.Light);
            Console.ReadLine();

            Console.WriteLine("Multicast");
            WorkPerformedHandler handler = logger + notify + downstream;

            DoWork(handler, 3, WorkType.Light);
        }
Example #29
0
        static void Main(string[] args)
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WordPerformed1);

            WorkPerformedHandler del2 = new WorkPerformedHandler(WordPerformed2);

            WorkPerformedHandler del3 = new WorkPerformedHandler(WordPerformed3);

            del1 = del2 + del2;

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

            del1(10, WorkType.GenerateReports);

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


            Console.Read();
        }
Example #30
0
        static void Main(string[] args)
        {
            // Delegate Instances:
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);

            // Adding to Invocation List:
            del1 += del2;
            del1(5, WorkType.GoToMeetings);

            MyDelegate f = func1;

            Console.WriteLine("The number is: " + f(10, 20));   // output is 30.

            // Reassign the function f:
            f = func2;
            Console.WriteLine("The number is: " + f(10, 20));   // output is 200.

            // To use the instanceMethod of class to implement delegates:

            // First create an instance of the class MyClass, then assign
            // the delegate variable to the instance method.
            MyClass mc = new MyClass();

            f = mc.instanceMethod1;
            Console.WriteLine("The number is: " + f(10, 20));   // output is 300.

            //Anonymous Delegates:
            MyDelegate AnonyF = delegate(int arg1, int arg2)
            {
                return((arg2 / arg1).ToString());
            };

            Console.WriteLine("The number is: " + AnonyF(10, 20));   // output: 2.

            Console.WriteLine("\nPress Enter to continue...");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            WorkPerformedHandler del1 = new WorkPerformedHandler(WorkPerformed1);
            WorkPerformedHandler del2 = new WorkPerformedHandler(WorkPerformed2);
            WorkPerformedHandler del3 = new WorkPerformedHandler(WorkPerformed3);

            //DoWork(del1);

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

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

            del1 += del2 + del3;

            //del1(10, WorkType.GoToMeetings);

            int finalHours = del1(10, WorkType.GenerateReports);

            Console.WriteLine(finalHours);

            Console.Read();
        }
Example #32
0
 // Make WorkPerformed(x) method dynamic
 static void DoWork(WorkPerformedHandler del)
 {
     del(5, WorkType.GenerateReports);
 }