Example #1
0
        static void Main(string[] args)
        {
            //SampleDelegate del1, del2, del3, del4;
            //del1 = new SampleDelegate(SampleMethodOne);
            //del2 = new SampleDelegate(SampleMethodTwo);
            //del3 = new SampleDelegate(SampleMethodThree);

            //del4 = del1 + del2 + del3;
            //del4 = del1 + del2 + del3 -del2;

            SampleDelegate del = new SampleDelegate(SampleMethodOne);

            del += SampleMethodTwo;
            del += SampleMethodThree;
            del -= SampleMethodOne;

            del();

            SampleReturnDelegate retDel = new SampleReturnDelegate(ReturnMethod1);

            retDel += ReturnMethod2;

            int result = retDel();

            Console.WriteLine(result);

            SampleOutDelegate outDel = new SampleOutDelegate(OutMethod1);

            outDel += OutMethod2;

            int resultOut = -1;

            outDel(out resultOut);
            Console.WriteLine(resultOut);

            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            // A.
            // THIS WILL WORK WHEN THE DELEGATES HAS A VOID TYPE AND BASED ON THAT
            // MEANS ALL THE METHODS REALTED TO THAT DELEGATES IS SAME TYPE

            // B.
            // WHEN THE METHODS/DELEGEATES HAS A RETURN TYPE THE LAST VALUE WILL BE RETRIEVED


            //----------------------------------A---------------------------------

            Console.WriteLine("-----------------A-------------------");
            // ------------------------------ WAY NR 1 --------------------------------------
            // ------------------------ MULTICASR DELEGATES ---------------------------------
            SampleDelegate del1, del2, del3, del4;

            // This Delegate is attached to SampleMethodOne
            del1 = new SampleDelegate(SampleMethodOne);
            // This Delegate is attached to SampleMethodTwo
            del2 = new SampleDelegate(SampleMethodTwo);
            // This Delegate is attached to SampleMethodThree
            del3 = new SampleDelegate(SampleMethodThree);
            // This Delegate is attached to SampleMethodOne + SampleMethodTwo + SampleMethodThree
            del4 = del1 + del2 + del3;  // + is to add(register) a method with DELEGTES
                                        // - is to delete(unregister) a method with DELEGTES

            // this delegate calling multi methods
            del4();


            // ------------------------------ WAY NR 2 --------------------------------------
            // ------------------------ MULTICASR DELEGATES ---------------------------------
            // there is no need to build multi delegates

            // You need to attach this delegare to one method at start
            SampleDelegate del = new SampleDelegate(SampleMethodOne);

            // += is to add(register) a method with DELEGTES
            // -= is to delete(unregister) a method with DELEGTES
            del += SampleMethodTwo;
            del += SampleMethodThree;
            Console.WriteLine("------------------------------------------");
            del();


            //----------------------------------B---------------------------------
            // INT METHODS
            SampleReturnDelegate delegate1, delegate2, delegate3;

            delegate1 = new SampleReturnDelegate(SampleReturnMethodOne);
            delegate2 = new SampleReturnDelegate(SampleReturnMethodTwo);

            delegate3 = delegate1 + delegate2;
            delegate3();
            Console.WriteLine("***************************************************");
            Console.WriteLine("-----------------B-------------------");
            Console.WriteLine("---INT METHODS---");
            Console.WriteLine(delegate3());


            // OUT METHODS
            Console.WriteLine("---OUT METHODS---");
            SampleOutDelegate DelOut;

            DelOut = new SampleOutDelegate(SampleOutOne);
            int delegateOutputParameter = -1;

            DelOut += SampleOutTwo;
            DelOut(out delegateOutputParameter);
            Console.WriteLine("The ouput variable is:{0}", delegateOutputParameter);
        }