• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#委托delegate的基本用法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

委托:就是一个方法的类型,下面3个调用情况来详细熟悉一下:

1.调用组合委托

    //委托:就是一个方法的类型
    public delegate int TestDelegateStr();
    public delegate string TestDelegateInt(int a);

    public  class 委托
    {
        //实例化委托:需要一个方法来实例化
        public static TestDelegateStr tdstr1;
        public static TestDelegateInt tdint  ;

        public static void main()
        {
            tdstr1 = testfunctionStr;
            tdstr1 = tdstr1 + testfunction;
            int result = tdstr1();  //调用组合委托
            Console.WriteLine("result" + result.ToString());

            tdint = testfunctionInt;
            tdint(1);
            Console.ReadKey();
        }

        public static int testfunction()
        {
            Console.WriteLine("111");
            return 1;
        }
        public static int testfunctionStr()
        {
            Console.WriteLine("222");
            return 2;
        }
        public static string testfunctionInt(int a)
        {
            Console.WriteLine("testfunction3");
            return " test";
        }
    }

 

2.委托之前的赋值:

        public delegate int CalculateDelegate(int a, int b);
        public void main()
        {
            CalculateDelegate cal;
            //让用户输入两个参数x和y
            //如果x>y,输出x-y
            //如果x<=y,输出x+y
            int x = 5; int y = 3;
            if (x > y)
            {
                cal = Minus;
            }
            else
            {
                cal = add;
            }
            int result= cal(x, y);
            Console.WriteLine(result.ToString());
        }

        public int add(int a, int b)
        {
            return a + b;
        }
        public int Minus(int a, int b)
        {
            return a - b;
        }
    }

 

3.委托delegate和Lambda之前的切换写法:

    public class 委托3
    {
        public delegate int CalculateDelegate(int a, int b);
        public delegate int CalculateDelegate2(int a);
        public void main()
        {
            CalculateDelegate cal;
            CalculateDelegate2 cal2;
            //让用户输入两个参数x和y
            //如果x>y,输出x-y
            //如果x<=y,输出x+y
            int x = 3; int y = 5;
            if (x > y)
            {
                cal = delegate (int a, int b) { return a - b; };  //匿名方法
            }
            else
            {
                //cal = delegate (int a, int b) { return a + b; };
                cal = (int a, int b) => { return a + b; };   //Lambda和上句等价
            }
            //简化1:如果Lambda方法体中只有一个返回值,那么大括号和return可以省略
            cal = (int a, int b) => a + b;

            //简化2:在Lambda的参数列表中,参数类型可以省略
            cal = (a, b) => a + b;

            //简化3:如果在Lambda参数列表中只有一个参数,那么参数的括号可以省略
            cal2 = a => a * a;



            int result= cal(x, y);
            Console.WriteLine(result.ToString());
        }
        
    }

 

 

4.使用委托实现异步执行

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap