参考  《编写高质量代码:改善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));
    }

 

相关文章:

  • 2021-10-03
  • 2022-12-23
  • 2022-02-19
  • 2021-08-04
  • 2022-01-03
  • 2022-12-23
  • 2021-09-15
猜你喜欢
  • 2022-12-23
  • 2022-03-06
  • 2021-09-08
  • 2018-08-22
  • 2021-11-20
  • 2022-12-23
  • 2020-06-04
相关资源
相似解决方案