C#委托 Lambda Func/Action

GitHub代码

  • 匿名方法
  • Action/Function
  • Lambda

在将讲Lambda之前需要先看看委托和匿名方法:

 匿名方法

using System;

namespace LambdaPractice
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var anonymous1 = new
            {
                Age = 1,
                ID = 1234567,
                Name = "Anonymous",
                Other = 1
            };
			Console.WriteLine("{0},{1},{2},{3}", anonymous1.Name, anonymous1.ID, anonymous1.Other, anonymous1.Age);
            Console.ReadKey();
        }
    }
}

 这就是一个匿名方法,可以看作是声明了一个anonymous的类,并给类中的属性赋值。匿名方法种类型必须为var。

 委托
 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。
 委托是一个类:可以实例化,而且必须实例化,不可以为静态的;他定义了方法的类型:委托也有类型,并且类型应该跟方法的类型保持一致;可以将方法当作另一个方法的参数来进行传递:方法应该作为委托的参数传递,并且方法和委托的参数个数以及参数类型要保持一致

 using System;

namespace LambdaPractice
{
    public class DelegatePractice
    {
    	//声明一个参数为int没有返回值的委托,这个委托可以接收参数为int没有返回值的方法
        public delegate void OnParamDelegate(int iParam);
		
		//声明一个参数为double返回值为string的委托,这个委托可以接收参数为double返回值为string的方法
        public delegate string OnParamWithReturnDelegate(double dParam);

        public void ShowDelegate()
        {
            {
            	//委托需要声明,参数为一个参数类型和委托参数类型相同的方法
                OnParamDelegate onParamDelegate = new OnParamDelegate(this.DoSomething);
                //如果有参数传入参数,如果没有参数则不传参
                onParamDelegate.Invoke(1);
				
				OnParamWithReturnDelegate onParamWithReturnDelegate = new OnParamWithReturnDelegate(this.DoSomething);
                onParamWithReturnDelegate.Invoke(1.2);
				
				//这样写也是对的
				OnParamWithReturnDelegate onParamWithReturnDelegate = this.DoSomething;
                onParamWithReturnDelegate.Invoke(1.2);
            }
        }

        private void DoSomething(int iParam)
        {
            Console.WriteLine("iParam:{0}", iParam);
        }

        public string DoSomething(double dParam)
        {
            return dParam;
        }
    }
}
=======================================================================
调用方法:
DelegatePractice delegatePractice = new DelegatePractice();
delegatePractice.ShowDelegate();

委托与匿名方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LambdaPractice
{
    public class DelegatePractice
    {
        public delegate void OnParamDelegate(int iParam);

        public void ShowDelegate()
        {
        	{
        		//委托和匿名方法的结合等同于上面我们写的委托加方法的形式,这可以说是匿名方法的特性
                OnParamDelegate onParamDelegate = new OnParamDelegate(delegate (int i)
                {
                    Console.WriteLine("iParam:{0}", i);
                });
				onParamDelegate.Invoke(1);
        	}
			{
				//随着C#特性的发展,委托也在慢慢改变 C#2.0中可以将delegate去掉同时需要使用)=>(goesto)
                OnParamDelegate onParamDelegate = new OnParamDelegate((int i) =>
                {
                    Console.WriteLine("iParam:{0}", i);
                });
                onParamDelegate.Invoke(1);
            }
            {
            	//C#3.0中省略匿名方法中的参数类型
				OnParamDelegate onParamDelegate = new OnParamDelegate((i) =>
                {
                    Console.WriteLine("iParam:{0}", i);
                });
                onParamDelegate.Invoke(1);
			}
        }
    }
}

 Action/Func
 在.net中有有一套系统自己的委托,我们可以不通过delegate来建立委托,其实我们通过查看c#中Action/Func的定义我们可以看见实际上系统已经使用delegate给我们创建了委托。
Action(没有返回值的委托):

using System;

namespace LambdaPractice
{
    internal class Program
    {
        private static void Main(string[] args)
        {
        	//无参
            Action actNoParam = new Action(() => { Console.WriteLine("NoParam"); });
            actNoParam.Invoke();
            
            //有参
            Action<int> actWithParam = new Action<int>((t) => { Console.WriteLine("{0}", t); });
            actWithParam.Invoke(1);
            
            Console.ReadKey();
        }
    }
}

Func(有返回值的委托):

using System;

namespace LambdaPractice
{
    internal class Program
    {
        private static void Main(string[] args)
        {
        	//有参
            Func<int> FuncNoParam = new Func<int>(() => { return 1; });
            FuncNoParam.Invoke();
            
            //无参
            Func<int, string> FuncWithParam = new Func<int, string>((t) => { return t.ToString(); });
            FuncWithParam.Invoke(123);

            Console.ReadKey();
        }
    }
}

 Lambda
 实际上在我们上面定义委托的时候我们已经使用到了Lambda表达式,C#的Lambda 表达式都使用 Lambda 运算符 =>,该运算符读为“goes to”。我们在以后Linq会经常使用到,不过样式会有点点不同。
 在Lambda中,如果参数只有一个可以省略参数外面的();如果为无参数的则直接使用();如果方法体只有一行,这可以省略方法体外的{}以及方法体的分号;

using System;

namespace LambdaPractice
{
    public class DelegatePractice
    {
       	Action actNoParam = new Action(() => { Console.WriteLine("NoParam"); });
    	Action<int> actWithParam = new Action<int>((t) => { Console.WriteLine("{0}", t); });
    	//1.如果参数只有一个可以省略参数外面的()
    	//Action<int> actWithParam = new Action<int>((t) => { Console.WriteLine("{0}", t); });
    	Action<int> actWithParam = new Action<int>(t => { Console.WriteLine("{0}", t); });
    	
    	//2.如果为无参数的则直接使用()
    	//Action actNoParam = new Action(() => { Console.WriteLine("NoParam"); });
    	Action actNoParam = new Action(() => { Console.WriteLine("NoParam"); });
    	
    	//3.如果方法体只有一行,这可以省略方法体外的{}以及方法体的分号;
    	Action actNoParam = new Action(() => Console.WriteLine("NoParam"));
    	Action<int> actWithParam = new Action<int>(t => Console.WriteLine("{0}", t));
		
		//4.我们再说委托的时候有说道在声明委托的时候,可以直接将方法赋给委托
		//OnParamWithReturnDelegate onParamWithReturnDelegate = this.DoSomething;
		Action actNoParam = () => Console.WriteLine("NoParam");
    	Action<int> actWithParam = t => Console.WriteLine("{0}", t);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值