C#委托的介绍(delegate、Action、Func、predicate)

static void Main(string[] args)
        {
            //Action
            TestAction<string>(Action, "Hello");
            TestAction<int>(Action, 1000);
            TestAction<string>(p => { Console.WriteLine(p); }, "Lamada");


            //Func
            int result = TestFunc<int, int>((t1, t2) => { return t1 + t2; }, 2, 6);
            Console.WriteLine(result);


            //predicate
            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
            Predicate<int> pre = delegate(int num) { return num > 3; };
            list.FindAll(pre).ForEach(n => { Console.WriteLine(n); });
            Console.ReadKey();
        }



        static void TestAction<T>(Action<T> action, T p)
        {
            action(p);
        }
        private static void Action(string s)
        {
            Console.WriteLine(s);
        }
        private static void Action(int s)
        {
            Console.WriteLine(s);
        }

        static int TestFunc<T1, T2>(Func<T1, T2, int> func, T1 t1, T2 t2)
        {
            return func(t1, t2);
        }
原文地址:https://www.cnblogs.com/tianboblog/p/5622237.html