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));
    }