func方法和委托方法总结

Func<string, string> checkAffairCode = oldAffairCodeStr =>
                       {
                           string newAffairCodeStr = "";
                           if (oldAffairCodeStr != "")
                           {
                               string[] affairCodeArr = oldAffairCodeStr.Split(';');
                               foreach (string affaircode in affairCodeArr)
                               {
                                   var affairsInfo = db.affairs.Where(x => x.AFFAIRCODE == affaircode).FirstOrDefault();
                                   if (affairsInfo != null)
                                   {
                                       newAffairCodeStr += newAffairCodeStr == "" ? affaircode : ";" + affaircode;
                                   }
                               }
                           }
                           return newAffairCodeStr;
                       };

调用方法:上面的方法是定义在方法中,并且可以在下面的代码中调用

官网地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.func-2?view=netframework-4.7.2

public ActionResult Index()
        {
            ces a = new ces() { id = 0, name = "default" };
            result re = new result(Result2);
            a = re.Invoke(a);//这句和下面一句的调用完全一样,写法不同而已
            a = re(a);
            result re2 = new result(Result3);
            a = re2.Invoke(a);
            result0 re3 = new result0(Result4);
            a = re3.Invoke(ref a);
            return View();
        }
        public static void Result1(ces a)
        {
            a.id = 1;
            a.name = "result2";
        }
        public static ces Result2(ces a)
        {
            a.id = 1;
            a.name = "result2";
            return a;
        }
        public static ces Result3(ces a)
        {
            a = new ces();
            a.id = 2;
            a.name = "result3";
            return a;
        }
        public static ces Result4(ref ces a)
        {
            a = new ces();
            a.id = 3;
            a.name = "result4";
            return a;
        }
        public delegate ces result(ces a);
        public delegate ces result0(ref ces a);

        public class ces
        {
            public int id { get; set; }
            public string name { get; set; }
        }

简单委托定义及调用,这个是在一次面试中出现过类似的问题,当时对于委托有些模糊

https://www.cnblogs.com/kewolf/p/4695889.html 

这篇文章讲解的比较详细

当存在多个参数的时候,比如获取两个int参数和的平方

            Func<int, int, int> func = (int1, int2) =>
            {
                return (int)Math.Pow(int1 + int2, 2);
            };
            int a = func(5, 2);//a=49

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值