Lambda表达式

作者:追风剑情 发布于:2020-1-9 13:57 分类:C#

Lambda表达式(C# 编程指南)
System.Linq.Expressions

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        delegate int del(int i);
        delegate int del1(int x, int y);
        delegate int del2();
        delegate void del3(string s);

        private static Action action;

        private static int k = 9;

        static void Main(string[] args)
        {
            /*
             Lambda 表达式:
             () => SomeMethod(); //使用括号指定0个参数
             x => x * x; //1个参数
             (x1, x2) => x1 * x2; //多个参数时用括号括起来
             (int x1, int x2) => x1 * x2; //编译器无法推断输入类型时,可以显示指定参数类型
             */
            del myDelegate = x => x * x;
            int j = myDelegate(5); //j = 25
            Console.WriteLine("j={0}", j);

            del1 myDelegate1 = (x, y) => x * y;
            int n = myDelegate1(4, 5);
            Console.WriteLine("n={0}", n);

            del2 myDelegate2 = () => SomeMethod();
            int m = myDelegate2();
            Console.WriteLine("m={0}", m);

            //条件查询
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
            int oddNumbers = numbers.Count(p => p % 2 == 1);
            Console.WriteLine("oddNumbers={0}", oddNumbers);

            /* Lambda 语句 */
            del3 myDelegate3 = s => { string s1 = s + " World"; Console.WriteLine(s1); };
            myDelegate3("Hello");

            //异步事件
            action += async () =>
            {
                await ExampleMethodAsync();
            };
            action();

            //表达式树 参见 https://www.cnblogs.com/ashleyboy/p/4831064.html

            //委托res引用返回x+1的方法
            Func<int, int> res = x => x + 1;
            //表达式树exp引用描述表达式x=>x+1的数据结构
            Expression<Func<int, int>> exp = x => x + 1;
            //将表达式树描述的lambda表达式编译为可执行代码,并生成表示该lambda表达式的委托
            Func<int, int> delf = exp.Compile();
            int q = delf(5);
            Console.WriteLine("q={0}", q);

            //通过API创建表达式树
            ParameterExpression param = Expression.Parameter(typeof(int), "x");
            ConstantExpression value = Expression.Constant(1, typeof(int));
            BinaryExpression body = Expression.Add(param, value);
            //public delegate TResult Func<in T, out TResult>(T arg);
            Expression<Func<int, int>> lambdatree = Expression.Lambda<Func<int, int>>(body, param);
            Console.WriteLine("参数param:{0}", param);
            Console.WriteLine("描述body:{0}", body);
            Console.WriteLine("表达式树:{0}", lambdatree);
            Func<int, int> delt = lambdatree.Compile();
            int t = delt(8);
            Console.WriteLine("t={0}", t);

            //条件表达式
            int num = 100;
            Expression conditionExpr = Expression.Condition(
                           Expression.Constant(num > 10),
                           Expression.Constant("num is greater than 10"),
                           Expression.Constant("num is smaller than 10")
                         );
            Console.WriteLine(conditionExpr.ToString());
            Console.WriteLine(Expression.Lambda<Func<string>>(conditionExpr).Compile()());

            Console.Read();
        }

        private static int SomeMethod()
        {
            return k;
        }

        private static async Task ExampleMethodAsync()
        {
            await Task.Delay(1000);//.NET 4.5+
            Console.WriteLine("ExampleMethodAsync");
        }
    }
}


运行测试

111.png

标签: C#

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号