C#中的委托与事件(一)

OK,我们了解C#中的委托与事件。
/*
 * 代理:简单来说,C#中的代理就是支持对方法的引用。代理和对象的引用十分相似,只是代理是用来指向某个方法,而不是对象。
 * 1.代理的定义:delegate int MyDelegate(int p,int q);这个例子定义了一个名为MyDelegate的代理,该代理类型的引用可以用来指向所有返回值为int类型
 * 且具有两个int类型参数的方法。
 * 2.代理的声明:MyDelegate arithMethod;该例声明了一个MyDelegate类型的代理引用。
 * 3.代理的使用:arithMethod=new MyDelegate(Add);//该代理的引用指向一个名为Add的方法。
 */
using System;
using System.Collections;
namespace Test
{
    class Program
    {
        static int Add(int a,int b)
        {
            return a + b;
        }
        delegate int MyDelegate(int a, int b);//定义一个代理
        static void Main(string[] args)
        {
            MyDelegate arithMethod = new MyDelegate(Add);//该代理的引用指向Add方法
            int result = arithMethod(1, 2);
            Console.WriteLine(result);
        }
    }
}

/*
 *在明白代理的引用调用实际方法后,就可以使用代理进行更灵活的操作。下面是一个使用代理根据用户选择不同的算术操作来引用不同的方法的例子。
 */
using System;
using System.Collections;
namespace Test
{
    class Program
    {
        delegate int MyDelegate(int a, int b);//定义一个委托

        static int Add(int a, int b)
        {
            return a + b;
        }

        static int Subtract(int a, int b)
        {
            return a - b;
        }

        static int Max(int a, int b)
        {
            return a > b ? a : b;
        }

        static void Main(string[] args)
        {
            MyDelegate arithMethod = null;
            Console.WriteLine("请输入参数A:");
            string strParaA = Console.ReadLine();
            Console.WriteLine("请输入参数B:");
            string strParaB = Console.ReadLine();
            int paraA = int.Parse(strParaA);
            int paraB = int.Parse(strParaB);
            Console.WriteLine("请输入运算符:+或-或m");
            char cOperand = (char)Console.Read();

            switch (cOperand)
            {
                case '+':
                    arithMethod = new MyDelegate(Add);
                    break;
                case '-':
                    arithMethod = new MyDelegate(Subtract);
                    break;
                case 'm':
                    arithMethod = new MyDelegate(Max);
                    break;
            }

            int result = arithMethod(paraA, paraB);
            Console.WriteLine(result);
        }
    }
}

/*
 *多重代理:C#可以使用一个代理同时指向多个符合方法标识的方法,这就是所谓的多重代理。
 *1.多重代理所定义的方法标识的返回值必须为void类型。例:delegate void MyDelegate(int a,int b);
 *2.如果多重代理引用多余一个方法,需要利用+=来完成新方法的添加。
 *例:MyDelegate arithMetjod;
 *    arithMethod=new MyDelegate(Add);
 *    arithMethod+=new MyDelegate(Subtract);
 *3.同样,我们可以利用-=来删除多重代理已引用的方法。
 *4.多重代理引用的所有方法都会被执行,多重代理的这个特点在后面的事件机制里得到了应用。
 */
using System;
using System.Collections;
namespace Test
{
    class Program
    {
        delegate void MyDelegate(int a, int b);//定义一个多重代理

        static void Add(int a, int b)//方法必须为void
        {
            Console.WriteLine(a + b);
        }

        static void Subtract(int a, int b)
        {
            Console.WriteLine(a - b);
        }

        static void Max(int a, int b)
        {
            Console.WriteLine(a > b ? a : b);
        }

        static void Main(string[] args)
        {
            MyDelegate arithMethod = new MyDelegate(Add);
            arithMethod += new MyDelegate(Subtract);
            arithMethod += new MyDelegate(Max);
            arithMethod(3, 4);//三个方法都会被执行
            arithMethod -= new MyDelegate(Max);//删去Max方法
            arithMethod(5, 4);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值