普通委托到泛型委托到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;
        }
    }

  

posted @ 2015-09-11 16:13  幽弥狂233  阅读(176)  评论(0编辑  收藏  举报