匿名委托Func—Action; 朗姆达表达式

来源:互联网 发布:中山大学附属医院 知乎 编辑:程序博客网 时间:2024/05/29 11:48

<1>

Action action //action是一个没有返回值,也没有参数的委托,相当于public delegate void action(),但还它也可以有带参数的,Action<string> action  //action是有一个带参数,没有返回值的委托,相当于public delegate void action(string str); //-------------------------------------------------------------------------Func<string,int> func //func是一个带参数而且带返回值的委托,尖括号里的最后一个参数就是委托的返回值类型,它相当于public delegate int func(string str);Func<int> func; //当这个func委托的尖括号里还有一个参数的时候,其实这个参数是委托的返回值类型,它相当于public delegate int func();


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 拉姆达表达式{    class Program    {               public void SayHello(Action action) //Action action是一个没有返回值,没有参数的委托,它相当于 public deletage void action()        {            action(); //执行这个委托        }        static void Main(string[] args)        {            Program p = new Program();            p.SayHello( //这里是调用SayHello方法                //实现委托,指定委托注册处理函数为Console.WriteLine()方法                () => { Console.WriteLine(" 哈哈"); } //既然这个委托没有参数,就可以写成()=> { 表达式 }。                );            Console.ReadKey();        }    }}

<2>

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 朗姆达表达式{    //“Lambda 表达式”是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式树类型    class Program    {        public delegate int del(int x);                static void Main(string[] args)        {            del mydel = (x) =>   //如果拉姆的这有一个参数的时候可以简写,即:del mydel=x=>x*x;            {                return x * x;            };            int i = mydel(5);            Console.WriteLine(i); //输出25            //------------------------------------------只有一个int参数,返回值为int类型的委托            Func<int, int> fun = x => x * x;            int j = fun(6);            Console.WriteLine(j); //输出36            Func<int, int> fun2 = (x) => //这一句与上面的fun委托效果是一样的。只是这里是全写,上面是简写            {                return x * x;            };            int j2 = fun2(6);            Console.WriteLine(j2); //输出36            //------------------------------------------只有2个int参数,返回值为int类型的委托            Func<int, int, int> fun3 = (x, y) => { return x * y; };            int j3=fun3(5, 6);            Console.WriteLine(j3); //输出30            //这是一个有2个int参数,返回值为int的委托,相当于 public delegate int fun4(int a, int b);            Func<int, int, int> fun4 = (int a,int b) =>            {                if (a > b)                {                    return a;                }                else                {                    return b;                }            };            int j4 = fun4(8, 18);            Console.WriteLine(j4);  //输出18            //与上文使用delegate定义匿名方法的作用相同,Lambda表达式的作用也是为了定义一个匿名方法。因此,下面使用delegate的代码和上面是等价的:            Func<int, int, int> fun5 = delegate(int a, int b)            {                if (a > b)                {                    return a;                }                else                {                    return b;                }            };            int j5 = fun4(8, 18);            Console.WriteLine(j5);  //输出18            //那么您可能就会问,这样看来Lambda表达式又有什么意义呢?Lambda表达式的意义便是它可以写的非常简单,例如之前的Lambda表达式可以简写成这样:            Func<int, int, int> max = (a, b) =>            {                if (a > b)                {                    return a;                }                else                {                    return b;                }            };            int j6 = max(8, 18);            Console.WriteLine(j6);  //输出18                                                    //-----------------------------------------只有一个参数,没有返回值的委托的朗姆达表达式            Action<int> action = (x) =>                {                    Console.WriteLine(x * x);                };            action(8); //调用action委托。输出64            Action<string> action3 = n => { string s = n + "你好"; Console.WriteLine(s); };            action3("Hi"); //调用action3委托。输出 Hi你好            //-----------------------------------------没有参数,没有返回值的委托的朗姆达表达式            Action action2 = () => { Console.WriteLine("中国"); };            action2(); //调用action2委托。输出 中国            //他相当于:先定义一个委托,然后让委托指向Console类下面的WriteLine方法            // public delegate void action2()              // action2 action22 = new Program.action2(Console.WriteLine);                       //-----------------------------------------------------------------------            int[] oddNumbers = { 8, 3, 5, 9, 15, 1, 6 };            //Count是返回一个数字,表示在指定的序列中满足条件的元素数量。            int result = oddNumbers.Count((s) => { return s % 2 == 1; }); //求 oddNumbers数组中各个元素除2取余等于1的数字个数                //oddNumbers.Where((r) =>                 //    {                //        return r % 2 == 1;                //    }).Count();            Console.WriteLine(result); //输出5            //FindAll是检索指定谓词定义的条件匹配的所有元素            List<int> slist = new List<int> { 1, 4, 2, 7, 3 };            var ss = slist.FindAll(d => d % 2 == 0);  //检索出slist数据集中除2等于0的所有元素            foreach (var item in ss)            {                Console.WriteLine(item);  //输出4,2            }                        Console.ReadKey();        }    }}


<3>

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 朗姆达表达式2{    class Program    {        static void Main(string[] args)        {            List<string> strList = new List<string>() { "1", "8", "5", "16", "2", "25" };            List<int> intList = new List<int>() { };            List<int> eveList = new List<int>() { };            List<int> squareList = new List<int>() { };            foreach (var item in strList)// 将strList的元素都转成int类型的整数,然后给他添加到intList中            {                intList.Add(int.Parse(item));            }            foreach (var item in intList)// 找出元素中所有偶数,然后给它添加到eveList集合中            {                if (item % 2 == 0)                {                    eveList.Add(item);                }            }            foreach (var item in eveList)  // 算出每个数的平方,然后给它添加到squareList集合中            {                squareList.Add(item * item);            }                       squareList.Sort();// 按照元素自身排序            foreach (var item in squareList)            {                Console.Write(item + " ");            }            //--------------------------------------------用朗姆达表达式实现            var list= strList                .Select(s => int.Parse(s)) // 将strList的元素都转成int类型的整数                .Where(y => y % 2 == 0)  // 找出元素中所有偶数                .OrderBy(z => z)  // 按照元素自身排序                .Select(a => a * a)  // 算出每个数的平方                .ToList();   //构造一个List            foreach (var item in list)            {                Console.Write(item + " ");            }            Console.Read();                    }    }}



0 0
原创粉丝点击