zoukankan      html  css  js  c++  java
  • 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);
            }
  • 相关阅读:
    二分
    枚举
    dp
    bfs
    bfs
    dfs
    ipython快捷键
    虚拟机串口连接嵌入式开发板
    rtmp向IR601移植过程(无功能步骤,只有移植步骤)
    静态库和动态库
  • 原文地址:https://www.cnblogs.com/tianboblog/p/5622237.html
Copyright © 2011-2022 走看看