zoukankan      html  css  js  c++  java
  • C#委托类型-(Func,Action,Predicate,lambda)

     C#委托的实现 除了 delegate 自定义委托类型外, 微软已经帮我们定义好了一些基本的委托类型

            //定义一个 函数  definedMethod 
            public static int definedMethod(object arg)
            {
                //Do Something
                return 0;
            }


    1. Func<T,...........,TResult) ,有返回值的委托类型

    T为参数类型

    TResult 为 返回至类型

                Func<string, int> funcMethod = delegate(string arg1)
                {
                    return 0;
                };
    使用已定义的函数definedMethod
                Func<object, int> funcMethod1 = definedMethod;

    2.Action<T,.....> ,无返回值委托类型

                Action<string> actionMethod = delegate(string arg1)
                {
                    //Do Something
                };

    3.Predicate<T,......>,返回值类型为布尔类型的 委托类型

                Predicate<string> predicateMethod = delegate(string arg1)
                {
                    //Do Something
                    return false;
                };


    4.lambda表达式的实现定义方法

    上述 代码实例中  都是使用 delegate 进行方法实体的定义

    下面 我们使用 lambda表达式 进行 定义

                Func<int, string, bool> lambda_test = (arg1, arg2) =>
                {
                    bool isTrue = true;
                    //Do Something
                    return isTrue;
                };

    当然如果 方法体中 没有太多代码要执行的话 也可以:

                Func<int, int, bool> lambda_test1 = (arg1, arg2) => arg1 > arg2;

    完整代码


        public class Test
        {
            //定义一个 函数  definedMethod (参数类型数量和返回值类型 与 定义的委托一致)
            public static string definedMethod(string arg)
            {
                //Do Something
                return arg;
            }
    
            public static void Main(string arg){
    
                //Func
                Func<string, string> funcMethod = delegate(string arg1)
                {
                    return arg1;
                };
                Func<string, string> funcMethod1 = definedMethod;
    
                funcMethod("Func 定义委托"); //===返回 "Func 定义委托"
                funcMethod1("Func 定义委托 definedMethod"); //===返回 "Func 定义委托 definedMethod"
    
                //Action
                Action<object> actionMethod = delegate(object arg1)
                {
                    //Do Something
                    arg1 = null;
                };
                object objarg1 = new object();
                actionMethod(objarg1); //===objarg1 值为null
    
    
                Predicate<bool> predicateMethod = delegate(bool arg1)
                {
                    //Do Something
                    return arg1;
                };
                predicateMethod(true); //==返回值 为True
    
    
                Func<int, string, string> lambda_test = (arg1, arg2) =>
                {
    
                    //Do Something
                    return arg1 + arg2;
                };
                lambda_test(1, "Lambda表达式");//===返回值 为 "Lambda表达式1"
    
                Func<int, int, bool> lambda_test1 = (arg1, arg2) => arg1 > arg2;
    
                lambda_test1(1, 2);//===返回值 为 false
                
            }
        }



  • 相关阅读:
    12.静态目录
    11.Git线上操作
    10.分离的前后台交互
    09.后台主页应用
    08.前端主页
    爬虫介绍
    python2与python3爬虫中get与post对比
    HTML+CSS+JavaScript
    数据库 Mysql-mongodb-redis
    时频工具箱介绍与使用
  • 原文地址:https://www.cnblogs.com/wycm/p/5338911.html
Copyright © 2011-2022 走看看