匿名方法和lambda表达式

namespace 匿名方法
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 匿名方法和lambda表达式
            /*
                //无参数无返回值的一个匿名方法
                MyDelegate md = delegate ()
                {
                    Console.WriteLine("hello");
                };
                md();

                //有参数无返回值的一个匿名方法
                MyDelegate1 md1 = delegate (string msg)
                {
                    Console.WriteLine(msg);
                };
                md1("world");

                //有返回值有多个参数的匿名函数
                MyDelegate2 ad = delegate (int n, int n2)
                {
                    return n + n2;
                };
                int res = ad(12, 32);
                Console.WriteLine(res);
            */

            ////没有参数没有返回值的方法使用lambda表达式
            //MyDelegate md = () => { Console.WriteLine("Lambda表达式"); };
            //md();

            ////有1参数没有返回值的方法使用lambda表达式
            //MyDelegate3 md = num => { Console.WriteLine(num); };
            //md(5);

            ////有返回值有多个参数的lambda表达式
            //MyDelegate2 md = (x, y) => { return x + y; };
            //int res = md(2, 3);
            //Console.WriteLine(res);

            ////参数可变的lambda表达式
            //AddDelegate ad = (arr) => 
            //{
            //    for (int i = 0; i < arr.Length; i++)
            //    {
            //        Console.WriteLine(arr[i]);
            //    }
            //    return arr.Sum();
            //};
            //int x = ad(1,2,3,4,54);
            //Console.WriteLine("求和结果为:"+x);
            //Console.ReadKey();
            #endregion

            #region 泛型委托,Action, Action<> ,Func 
            ////1  需要保存一个无参数,无返回值的方法,系统自带
            //Action ac1 = new Action(M1);
            //ac1();
            //Console.ReadKey();

            //2 需要保存多个不同类型参数,没有返回值的泛型方法
            //MyGenericDelegate<string> md = M1;
            //md("hello");
            //Console.ReadKey();
            //MyGenericDelegate<int > md = M1;
            //md(12);
            //Console.ReadKey();

            //Action委托,的非泛型版,就是一个无参数,无返回值的的委托
            //Action的泛型版本,就是一个无返回值,但是参数可变的委托。
            //Action<string> md = M1;    //M1函数自己写    
            //md("hello");
            //Console.ReadKey();

            ////Lambda Action<>泛型,传参就可以了,不用自己写类似M1函数
            //Action<string> md = m => { Console.WriteLine(m); };
            //md("hello");

            ////Lambda Action<>泛型,传参就可以了,不用自己写类似M1函数
            //Action<int, int,int,int,int> a2 = (x, y,z,w,h) => { Console.WriteLine(x+y+z+w+h); };
            //a2(2, 5,8,9,12);

            ////Lambda Action<>泛型,传参就可以了,不用自己写类似M1函数
            //Action<char, int, string, int, double> a2 = (x, y, z, w, h) => { Console.WriteLine(x.ToString() + y + z.ToString() + w + (double)h); };
            //a2('w', 5, "字符串", 9, 1.2);
            //Console.ReadKey();

            ////Func委托只有一个泛型版,没有非泛型版本的。
            //Func<string>如果有一个参数则就是返回值 
            //Func<int,string>则第一个int是参数,最后string是返回值 ,依次类推。
            //Func<int, int, int, int, int> aa = M3; //M3 函数自己写
            //int res = aa(2,3,4,4);
            //Console.WriteLine(res);
            //Console.ReadKey();

            ////Lambda Func<>
            //Func<int> f = () => { return 100; };
            //int res = f();
            //Console.WriteLine(res);
            //Console.ReadKey();

            ////Lambda Func<>
            // Func<int, int, int> Fd = (a, b) => { return a + b; };
            //int res =  Fd(2, 3);
            // Console.WriteLine(res);
            // Console.ReadKey();
            #endregion

            #region 使用Lambda表达式
            List<int> lst = new List<int>() { 1, 2, 5, 6, 43, 4, 22, 4, 65, 75, 23 };
            IEnumerable<int> ie = lst.Where(x => { return x > 10; });
            foreach (var item in ie)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

            #endregion
        }
        static  int M3(int a,int b,int c ,int d)
        {
            return a + b + c + d;
        }
        static void M1(int m)
        {
            Console.WriteLine(m);
        }
        static void M1(string msg)
        {
            Console.WriteLine(msg);
        }
        static void M1()
        {
            Console.WriteLine("Hello world");
        }
    }
    //声明委托
    public delegate int AddDelegate(params int[] arr);
    public delegate int MyDelegate2(int n1,int n2);
    public delegate void MyDelegate1(string msg);
    public delegate void MyDelegate();
    public delegate void MyDelegate3(int num);

    //声明一个泛型委托
    public delegate void MyGenericDelegate<T>(T args);
}
转载请注明出处:  https://www.cntworld.cn
智能工控 » 匿名方法和lambda表达式

发表回复

提供最优质的资源集合

立即查看 了解详情