C# 委托——delegate-02

C# 委托——delegate

委托是方法的抽象,它存储的就是一系列具有相同签名和返回回类型的方法的地址;

简单理解:
委托从字面上理解就是一种代理,类似于房屋中介,由租房人委托中介为其租赁房屋。

使用步骤“
委托在使用时遵循三步走的原则,即定义声明委托、实例化委托以及调用委托。
委托是 C# 语言中的一个特色,通常将委托分为命名方法委托、多播委托、匿名委托,其中命名方法委托是使用最多的一种委托。

语法:
修饰符 delegate 返回值类型 委托名 ( 参数列表 );

实例化委托的语法
委托名 委托对象名 = new 委托名 ( 方法名 );

实例化委托后即可调用委托
委托对象名 ( 参数列表 );

基础用法:

 public class FuncClass
    {
        //第一步:定义一个委托
        //修饰符  delegate  返回值类型  委托名 ( 参数列表 );
        public delegate string Mydelegate(); 
         public delegate string Mydelegate1(int a,int b,int c);
        public static void getinit()
        {
            //第二步:实例化委托
            //委托名  委托对象名 = new 委托名 ( 方法名 );
            Mydelegate mydelegate = new Mydelegate(returnstring);

            //第三步:调用委托
            //委托对象名 ( 参数列表 ); 
            mydelegate();
            Console.WriteLine(mydelegate());
             Mydelegate1 mydelegate1 = new Mydelegate1(returnint);
             Console.WriteLine(mydelegate1(1,2,3));
        }
        public static string returnstring()
        {
            return "测试返回string类型的Func";
        }
        public static string returnint(int a,int b,int c)
        {
            int d= a + b + c;
            return d.ToString() ;
        } 
    }

demo:

 public class FuncClass
    {
        //第一步:定义一个委托
        //修饰符  delegate  返回值类型  委托名 ( 参数列表 ); 
        public delegate string Mydelegate1(Func<int,int,int,int> func);
        public static void getinit()
        {
            //第二步:实例化委托
            //委托名  委托对象名 = new 委托名 ( 方法名 );
            Mydelegate1 mydelegate2 = new Mydelegate1(returnint);

            //第三步:调用委托
            //委托对象名 ( 参数列表 ); 
            mydelegate2((a, b, c) => a * b * c);
            Console.WriteLine(mydelegate2((a, b, c) => a * b * c));
              
        }
        public static string returnstring()
        {
            return "测试返回string类型的Func";
        }
        public static string returnint(int a, int b, int c)
        {
            int d = a + b + c;
            return d.ToString();
        }
        //Func<int,int,int,int> func:lambda表达式
        public static string returnint(Func<int,int,int,int> func)
        {
            string a="";
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(   func(i, i + 2, i + 4));
                a = func(i, i + 2, i + 4).ToString();
            }

            return a;
        } 
    }

demo:
在这里插入图片描述
多播委托

 public class FuncClass
    {
        //第一步:定义一个委托
        //修饰符  delegate  返回值类型  委托名 ( 参数列表 );
        public delegate string Mydelegate(); 
        public static void getinit()
        { 
            //第二步:实例化委托
            //委托名  委托对象名 = new 委托名 ( 方法名 );
            Mydelegate mydelegate1 = new Mydelegate(returnint01); 
            //多播委托
            mydelegate1 += returnint02;
            mydelegate1 += returnint03;
            mydelegate1 += returnint03;
            mydelegate1 += returnint03;
            mydelegate1 -= returnint03;
            //多播委托数组
            Delegate[] delegates = mydelegate1.GetInvocationList(); 
            foreach (Mydelegate dd in delegates)
            {
                //执行委托
                Console.WriteLine(dd());
            }
        }
        public static string returnint01()
        {
            return "测试多播委托";
        }
        public static string returnint02()
        { 
            return "测试多播委托+";
        }
        public static string returnint03()
        { 
            return "测试多播委托-";
        }

    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值