• C#泛型委托Predicate、Action、Func


    Predicate

    Predicate泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
    通过查看源码发现

    Predicate  Array

    先来看一下Array.FindAll

    构建一个字符串类型的数组进行演示测试

    第一种方式

                string[] arrayString = new string[]
                                         {
                                             "One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
                                         };
                string[] arrayResult = Array.FindAll(arrayString, (c) => c.Length > 3);

    如上图所示FindAll两个参数第一个就是数组(字符串数组,int数组等等),第二个就是委托Predicate有一个参数。

    上面的演示测试功能就是 测试字符串数组中的项的长度大于3的就返回,可以看出应该是有6个

    下面来看一下测试结果

    (c) => c.Length > 3 此处这就是一个lambda表达式
    第二种方式
            public void PredicateArrayTest()
            {
                string[] arrayString = new string[]
                                         {
                                             "One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
                                         };
                string[] arrayResult = Array.FindAll(arrayString, (c) => c.Length > 3);
                string[] arrayResultMethod = Array.FindAll(arrayString, GetString);
            }
            private bool GetString(string item)
            {
                if (item.Length > 3)
                {
                    return true;
                }
                return false;
            }

    通过一个GetString方法,也就是通过委托进行传递方法的方式

    查看效果,结果是一样的

    第三种方式
     string[] arrayResultDelegate = Array.FindAll(arrayString, delegate(string c) { return c.Length > 3; });

    直接通过匿名代理可以达到同样的效果。

    查看源代码可以发现,Array数组的其他很多方法同样都使用了Predicate委托

    再写一个简单的小例子进行巩固一下对Precidate委托的使用

            public string PredicateNewTest(Predicate<string> item )
            {
                string[] arrayString = new string[]
                                         {
                                             "One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
                                         };
                foreach (string str in arrayString)
                {
                    if (item(str))
                    {
                        return str;
                    }
                }
                return null;
    
                //return arrayString.FirstOrDefault(str => item(str));
            }
            [TestMethod]
            public void PredicateNewTest()
            {
                string str = PredicateNewTest((c) => { return c.Length > 3; });
            }

    通过下面的函数进行调用之后返回

    Predicate  List

    对List的实现其实和Array的实现原理几乎完全一样,暂时就不实现了,可以自己想想吧,之后看情况,自己再补上。

    Func

    可以很清楚的看出,封装一个带有返回 TResult 参数指定的类型值的方法,它有多个重载。下面我们通过委托来处理一下3*5=15的过程。

            public delegate string MultiTest(int a, int b);
            [TestMethod]
            public  void ActionTest()
            {
                MultiTest muliti = new MultiTest(MultiMethod);
                string result=muliti(3, 5);
            }
    
            public string MultiMethod(int a,int b)
            {
                return (a*b).ToString();
            }

    其中:定义一个有两个int类型参数的,返回值为字符串的委托。声明一个乘法的方法,并且返回字符串,然后调用。

    那么现在我们来使用Func来优化的代码是什么样呢

            public string MultiMethod(int a,int b)
            {
                return (a*b).ToString();
            }
    
            [TestMethod]
            public  void FuncTest()
            {
                Func<int,int,string> func=new Func<int, int, string>(MultiMethod);
                string result = func(3, 5);
            }

    结果为

    是不是很简单呢,那么你自己也试一下吧。

    Action

    Action与Func极为类似,只不过Action执行没有返回值而已,那么应该就更简单一些了,在此就不进行示例讲解了。

     
  • 相关阅读:
    Vue中 el-table大数据量加载,不分页,节省内存的性能优化
    http请求中Content-Type以及qs.stringify的使用
    setTimeout用法(Event Loop简介、for循环中应用、vue中应用)
    Vue中关于图片路径配置的填坑日记
    WebSocket
    Vue中mockjs的使用
    Vue 作用域插槽slot slot-scope v-slot
    Windows Server 2003搭建邮件服务器
    Exchange 2010的部署
    Exchange 2010 详细安装步骤
  • 原文地址:https://www.cnblogs.com/aehyok/p/3382291.html
Copyright © 2020-2023  润新知