Example #1
0
        static void Main(string[] args)
        {
            SampleDelegate samp4, samp1, samp2, samp3;
            SampleDelegate del1 = new SampleDelegate(SampleMethodOne);

            del1 += SampleMethodTwo;
            del1();

            samp1 = new SampleDelegate(SampleMethodOne);
            samp2 = new SampleDelegate(SampleMethodTwo);
            samp3 = new SampleDelegate(SampleMethodThree);

            samp4 = samp1 + samp2 + samp3;

            samp4();

            Console.WriteLine("------------");
            SampleDelegate1 Sdel1 = new SampleDelegate1(SampleMOne);

            Sdel1 += SampleMTwo;

            int DelegateRetValue;

            Sdel1(out DelegateRetValue);
            Console.WriteLine("Value of Delegate {0}", DelegateRetValue);


            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            SampleDelegate samp4, samp1, samp2, samp3;
            SampleDelegate del1 = new SampleDelegate(SampleMethodOne);
            del1 += SampleMethodTwo;
            del1();

            samp1 = new SampleDelegate(SampleMethodOne);
            samp2 = new SampleDelegate(SampleMethodTwo);
            samp3 = new SampleDelegate(SampleMethodThree);

            samp4 = samp1 + samp2 + samp3 ;

            samp4();

            Console.WriteLine("------------");
            SampleDelegate1 Sdel1 = new SampleDelegate1(SampleMOne);
            Sdel1 += SampleMTwo;

            int DelegateRetValue ;
            Sdel1(out  DelegateRetValue);
            Console.WriteLine("Value of Delegate {0}", DelegateRetValue);

            Console.ReadLine();
        }
Example #3
0
        static void Main(string[] args)
        {
            // Approach 1:
            SampleDelegate1 del1 = new SampleDelegate1(SampleMethodOne);
            SampleDelegate1 del2 = new SampleDelegate1(SampleMethodTwo);
            SampleDelegate1 del3 = new SampleDelegate1(SampleMethodThree);
            // In this example del4 is a multicast delegate. You use +(plus)
            // operator to chain delegates together and -(minus) operator to remove.
            SampleDelegate1 del4 = del1 + del2 + del3 - del2;

            del4();

            // Approach 2:
            // In this example del is a multicast delegate. You use += operator
            // to chain delegates together and -= operator to remove.
            SampleDelegate2 delAp2 = new SampleDelegate2(SampleMethodOne);

            delAp2 += SampleMethodTwo;
            delAp2 += SampleMethodThree;
            delAp2 -= SampleMethodTwo;
            del2();

            // Multicast delegate with an int return type:
            SampleDelegate3 delAp3 = new SampleDelegate3(SampleMethodOneInt);

            delAp3 += SampleMethodTwoInt;


            // The ValueReturnedByDelegate will be 2, returned by the SampleMethodTwo(),
            // as it is the last method in the invocation list.
            int ValueReturnedByDelegate = delAp3();

            Console.WriteLine("Returned Value = {0}", ValueReturnedByDelegate);

            // Multicast delegate with an integer output parameter:
            SampleDelegate4 delAp4 = new SampleDelegate4(SampleMethodOne);

            delAp4 += SampleMethodTwo;


            // The ValueFromOutPutParameter will be 2, initialized by SampleMethodTwo(),
            // as it is the last method in the invocation list.
            int ValueFromOutPutParameter = -1;

            delAp4(out ValueFromOutPutParameter);
            Console.WriteLine("Returned Value = {0}", ValueFromOutPutParameter);
        }