C#进阶语法-委托和Lambda

自定义委托

//委托方法

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

//委托方法的定义

 delegate int AddDelegate(int a, int b);

//委托初始化(方法名)

  AddDelegate addDelegate = new AddDelegate(Add);

//委托的调用(方法参数)

 int c = AddDelegate.Invoke(3, 4);

代码

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​
    class Program
    {
​
        class Class1
        {
            static void Main(string[] args)
            {
                AddDelegate AddDelegate = new AddDelegate(Add);
                int c = AddDelegate.Invoke(3, 4);
                Console.WriteLine(c);
            }
​
            public static int Add(int a, int b)
            {
                int c = a + b;
                return c;
            }
        }
​
        delegate int AddDelegate(int a, int b);
    }
}
​

泛型委托

解决代码重用,写出通用委托

//泛型委托方法

 public static void WXKINT(int a ,int b)
            {
​
                int c = a + b;
                Console.WriteLine(c);
            }
​
 public static void  WXKDOUBLE(double a, double b)
            {
                double c = a + b;
                Console.WriteLine(c);
            }

//泛型委托的定义

 delegate void WXKDelegate<T>(T a, T b);

//委托初始化(方法名)

 WXKDelegate <int> wXKDelegate = new WXKDelegate<int>(WXKINT);

//委托的调用(方法参数)

      wXKDelegate(2, 5);//调运委托可以不加Invoke

代码

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​
    class Program
    {
​
        class Class1
        {
            static void Main(string[] args)
            {
​
                WXKDelegate <int> wXKDelegate = new WXKDelegate<int>(WXKINT);
                wXKDelegate(2, 5);//调运委托可以不加Invoke
                     
            }
​
​
            public static void WXKINT(int a ,int b)
            {
​
                int c = a + b;
                Console.WriteLine(c);
            }
​
            public static void  WXKDOUBLE(double a, double b)
            {
                double c = a + b;
                Console.WriteLine(c);
            }
        
        }
     
        delegate void WXKDelegate<T>(T a, T b);
       
    }
}

预定义委托

不需要定义委托

//委托方法

  public static void Show(string a ,int num)
            {
​
               
                Console.WriteLine(a);
            }
​
 public static void  SHOW2(int a,int b,int c)
            {
                Console.WriteLine(c);
            }
​
 public static string SHOW3()
            {
                return "有返回值";
            }
public static int SHOW4(int a)
            {
                return a;
            }
        

//委托不需定义

//委托初始化和调用(方法名)

                 //无返回值
                Action<string, int> action = new Action<string, int>(Show);
                action("无返回值",1);
​
​
                Action<int, int,int> action2 = new Action<int,int,int>(SHOW2);
                action2(1,2,3);
​
                //有返回值
                Func<string> func1 = new Func<string>(SHOW3);
                Console.WriteLine(func1());
​
                Func<int, int> func2 = new Func<int, int>(SHOW4);
               
                Console.WriteLine(func2(111));

代码

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​
    class Program
    {
​
        class Class1
        {
            static void Main(string[] args)
            {
                //无返回值
                Action<string, int> action = new Action<string, int>(Show);
                action("无返回值",1);
​
​
                Action<int, int,int> action2 = new Action<int,int,int>(SHOW2);
                action2(1,2,3);
​
                //有返回值
                Func<string> func1 = new Func<string>(SHOW3);
                Console.WriteLine(func1());
​
                Func<int, int> func2 = new Func<int, int>(SHOW4);
               
                Console.WriteLine(func2(111));
                Console.ReadKey();
            }
​
            public static string SHOW3()
            {
                return "有返回值";
            }
            public static int SHOW4(int a)
            {
                return a;
            }
​
            public static void Show(string a ,int num)
            {
​
               
                Console.WriteLine(a);
            }
​
            public static void  SHOW2(int a,int b,int c)
            {
                Console.WriteLine(c);
            }
        
        }
       
    }
}
​

lambda

本质是匿名方法 ,让我们更快更简单完成委托,还可以让实例化委托的方法访问局部变量

起初代码是这样

 //最初是这种复杂分开写
                Action<string> action = new Action<string>(Show);
                action("我是泛型委托Action");
//还要在下面写委托方法
​
            public static void Show(string a)
            {
​
               
                Console.WriteLine(a);
            }
​
       

第一步演变

  //使用匿名方法
         Action<string> action2 = new Action<string>(delegate (string msg)
                {
                    Console.WriteLine(msg);
                });
                action2("我是委托action,使用了匿名方法");

接下来更简单一下

​
                //=>念成gose to
                Action<string> action3 = new Action<string>( (string msg)=>
                {
                    Console.WriteLine(msg);
                });
                action3("我是委托action,使用了匿名方法");

更简单的写下来

  //一个参数 一个语句的方法
                Action<string> action4 = new Action<string>((msg) => Console.WriteLine(msg));
                action4("我是委托action,使用了匿名方法");

接下来就是lambda

 //一个参数 一个语句的方法
      Action action5 = new Action(() => Console.WriteLine("无返回值,无参数"));
      action5();
   //有返回值,单条语句
              Func<string> func6 = new Func<string>(() => "有返回值,单条语句");
              func6();
   //有返回值,多条语句
                Func<string> func7 = new Func<string>(() =>
                {
                    Console.WriteLine("有返回值,多条语句");
                    Console.WriteLine("有返回值,多条语句");
                    Console.WriteLine("有返回值,多条语句");
                    Console.WriteLine("有返回值,多条语句");
                    return "有返回值,多条语句";
                });
                func7();

代码:

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​
    class Program
    {
​
        class Class1
        {
            static void Main(string[] args)
            {
                //最初是这种复杂分开写
                Action<string> action = new Action<string>(Show);
                action("我是泛型委托Action");
​
                //使用匿名方法
                Action<string> action2 = new Action<string>(delegate (string msg)
                {
                    Console.WriteLine(msg);
                });
                action2("我是委托action,使用了匿名方法");
​
                //=>念成gose to
                Action<string> action3 = new Action<string>( (string msg)=>
                {
                    Console.WriteLine(msg);
                });
                action3("我是委托action,使用了匿名方法");
​
                //一个参数 一个语句的方法
                Action<string> action4 = new Action<string>((msg) => Console.WriteLine(msg));
                action4("我是委托action,使用了匿名方法");
​
                //无返回值 无参数
                //一个参数 一个语句的方法
                Action action5 = new Action(() => Console.WriteLine("无返回值,无参数"));
                action5();
                //有返回值,单条语句
                Func<string> func6 = new Func<string>(() => "有返回值,单条语句");
                func6();
                //有返回值,多条语句
                Func<string> func7 = new Func<string>(() =>
                {
                    Console.WriteLine("有返回值,多条语句");
                    Console.WriteLine("有返回值,多条语句");
                    Console.WriteLine("有返回值,多条语句");
                    Console.WriteLine("有返回值,多条语句");
                    return "有返回值,多条语句";
                });
                func7();
            }
​
            public static void Show(string a)
            {
​
               
                Console.WriteLine(a);
            }
​
       
        
        }
       
    }
}
​
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值