• c# 三种常见的委托


      参考  《编写高质量代码:改善C#程序的157个建议》 , 尽量使用FCL中的委托声明。

         FCL: FrameWork Class Library

     三种常用:Action、Func、Predicate

    小例子:

    1.Action :无返回值

        private void AddAction(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        static void Main(string[] args)
        {
            TestCSharpClass myTest = new TestCSharpClass();
            Action<int, int> testAction = myTest.AddAction;
            testAction(1, 2);
        }

    2.Func<T1,T2....,out Tn>:有返回值,

        private int AddFuncTest(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            TestCSharpClass myTest = new TestCSharpClass();
            Func<int, int, int> funcTest = myTest.AddFuncTest;
            Console.WriteLine(funcTest(1,2));
        }

    3.Predicate<T>  返回bool值。 一个参数。  在查询接口中比较有用

        private class PreTestClass
        {
            private int a;
            private int b;
            public PreTestClass(int a0, int b0)
            {
                a = a0;
                b = b0;
            }
            public bool TestBool(int ccc)
            {
                if (a + b >= ccc)
                {
                    return true;
                }
                return false;
            }
        }
        static void Main(string[] args)
        {
            PreTestClass testClass = new PreTestClass(1, 2);
            Predicate<int> testPredicate = testClass.TestBool;
            Console.WriteLine(testPredicate(4));
        }
    改变自己
  • 相关阅读:
    Ubuntu下ClickHouse安装
    redis.conf配置详解(转)
    php使用sftp上传文件
    ubuntu下安装nginx1.11.10
    cookie和session的区别
    linux下Redis主从复制
    linux-ubuntu 安装配置Redis
    php的常量
    Ubuntu防火墙配置
    技术资料
  • 原文地址:https://www.cnblogs.com/sun-shadow/p/6165657.html
Copyright © 2020-2023  润新知