示例#1
0
        private static void Main(string[] args)
        {
            // Refer to the Increment method by instantiating the delegate
            // and passing the method itself in as an argument
            IncrementDelegate inc = new IncrementDelegate(Increment);

            Console.WriteLine(inc());  // => 1

            // Delegates can be composed with the + operator
            IncrementDelegate composedInc = inc;

            composedInc += inc;
            composedInc += inc;

            // composedInc will run Increment 3 times
            Console.WriteLine(composedInc());  // => 4

            // Subscribe to the event with the delegate
            MyEvent += new IncrementDelegate(Increment);
            MyEvent += new IncrementDelegate(Increment);

            // Trigger the event
            // ie. run all delegates subscribed to this event
            Console.WriteLine(MyEvent());  // => 6
        }
示例#2
0
            public unsafe OutOfBoundsVbtl(uint outOfBounds) : base(1)
            {
                var @delegate = new IncrementDelegate(IncrementImpl);
                var fnPtr     = (delegate * managed <IntPtr, int, int>) & IncrementImpl;

                AddMethod(@delegate, 0);
                switch (outOfBounds)
                {
                case 1:
                    AddMethod(@delegate, 1);
                    break;

                case 2:
#pragma warning disable 618
                    AddMethod(@delegate);
#pragma warning restore 618
                    break;

                case 3:
                    AddMethod(@delegate, 42);
                    break;

                case 4:
                    AddMethod(fnPtr, 42u);
                    break;

                case 5:
                    AddMethod(fnPtr, 0u);
                    break;
                }
            }
示例#3
0
    public static void Main()
    {
        IncrementDelegate[] values = { Incrementer, Incrementer, Incrementer, Incrementer, Incrementer };
        IncrementDelegate   del    = (IncrementDelegate)
                                     IncrementDelegate.Combine(values);
        long  result = 1;
        short count  = 1;

        foreach (IncrementDelegate number in del.GetInvocationList())
        {
            result = result * number(ref count);
        }
        Console.WriteLine("{0} factorial is {1}", del.GetInvocationList().Length, result);
    }