zoukankan      html  css  js  c++  java
  • 普通委托到泛型委托到Linq

           private delegate bool delTest(int a);
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                var arr = new List<int>() { 1, 2, 3, 4, 5, 6 }; //集合初始化器
    
                //var d1 = SomethingFactory<int>.InitInstance(12);
    
                var d1 = new delTest(More);                                     //默认委托
    
                var d2 = new Predicate<int>(More);                              //带一个参数的泛型委托
    
                var d3 = new Action<int, string>(twoParamNoReturnAction);       //带多个参数的泛型委托,有返回值
    
                var d4 = new Func<int, string>(oneParamOneReturnFunc);          //带多个参数的泛型委托,无返回值
    
                var d5 = new Action<int, string>(delegate(int item, string item1) { Console.WriteLine(item.ToString() + "," + item1);}); //匿名方法
    
                var d6 = new Action<int,string>((item,item1)=> Console.WriteLine(item.ToString() + "," + item1)); //Lambda表达式
    
                var d7 = arr.Where(delegate(int a) { return a > 3; }).Sum();    //Line Where调用一个 Func<int,bool>类型的泛型委托
    
                var d8 = arr.Where(a => { return a > 3; }).Sum();               //Linq Where用Lambda表达式表示的泛型委托
    
                var d9 = (from v in arr where v > 3 select v).Sum();            //Linq语句
                
            }
    
            static void noParamNoReturnAction()
            { 
                //do what you want
            }
    
            static void twoParamNoReturnAction(int a, string b)
            { 
                //do what you want
            }
    
            static string oneParamOneReturnFunc(int a)
            {
                return string.Empty;
            }
    
            static bool More(int item)
            {
                if (item > 3)
                {
                    return true;
                }
                return false;
            }
    
            static bool Less(int item)
            {
                if (item < 3)
                {
                    return true;
                }
                return false;
            }
        }
    

      

  • 相关阅读:
    mysql:添加索引
    mysql: update字段中带select
    ASP.NET Web API 2 入门
    notify()、notifyAll()和wait()
    Mybatis3 框架理解
    项目中用到的Java注解
    使用webservice实现App与服务器端数据交互
    IntelliJ idea 14 集成 tomcat 7
    使用adb签名并安装Android程序
    写了一个月的单元测试,总算明白大学里这门课白学了
  • 原文地址:https://www.cnblogs.com/hdsong/p/4801242.html
Copyright © 2011-2022 走看看