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

      

  • 相关阅读:
    The EF 6.x DbContextGenerator templates are not available for VS2010
    selenium定位tr及td,并获取其文本及属性
    selenium基本操作
    Git入门
    独热编码OneHotEncoder简介
    openCV中直方图均衡化算法的理解
    pyinstaller生成exe文件失败
    图像形态学运算
    对双边滤波的理解
    PyQt中对RadioButton分组
  • 原文地址:https://www.cnblogs.com/hdsong/p/4801242.html
Copyright © 2011-2022 走看看