C# 学习笔记 12. Lambda表达式

1.什么是Lambda表示式

//c++也有lambda的用法匿名函数

可以理解为一个匿名的方法,可以包含表达式和语句。

lambda 都会使用 =>运算符,左边是匿名方法的输入参数,右边是是表示式

或语句块。

//例子

// public delegate TResult Func<in T, out TResult>(T arg);  

//Func是系统已经定义过的委托

 class Program
    {
        static void Main(string[] args)
        {
            Func<string, string> f1 = new Func<string, string>(callback);

            Func<string, string> f2 = delegate(string name)
            {
                return name;
            };

            Func<string, string> f3 = (string name) => name;
            //省略参数类型和()
            Func<string, string> f4 = name => name;

            Console.WriteLine(f1("hello f1"));
            Console.WriteLine(f2("hello f2"));
            Console.WriteLine(f3("hello f3"));
            Console.WriteLine(f4("hello f4"));
            Console.ReadKey();

        }
        private static string  callback(string name)
        {
            return name ;
        }
    }

//使用对象初始化器+lambda

class Program
    {
        static void Main(string[] args)
        {
            //2.0
            //             Button btn1 = new Button();
            //             btn1.Text = "click me ";
            // 
            //             //匿名订阅
            //             btn1.Click += delegate (object sender, EventArgs e)
            //             {
            //                 ReportEvent("click", sender, e);
            //             };
            //             btn1.KeyPress += delegate (object sender, KeyPressEventArgs e)
            //             {
            //                 ReportEvent("KeyPress", sender, e);
            //             };
            //             Form form = new Form();
            //             form.Name = "控制台窗体程式";
            //             form.AutoSize = true;
            //             form.Controls.Add(btn1);
            //             Application.Run(form);
            //3.0
            Button btn1 = new Button() { Text = "点我" };
            //订阅
            btn1.Click += (sender, e) => ReportEvent("click",sender,e);
            btn1.KeyPress += (sender, e) => ReportEvent("keypress", sender, e);

            //
            Form form = new Form { Name = "控制台创建窗体程序", AutoSize = true, Controls = { btn1 } };

            Application.Run(form);

            //Console.ReadKey();
        }

        private static void ReportEvent(string title,object sender,EventArgs e)
        {
            Console.WriteLine("事件:{0}", title);
            Console.WriteLine("事件:{0}", sender);
            Console.WriteLine("事件:{0}", e.GetType());

        }


    }

2.表示式树

Lambda表示式除了可以用来创建委托以外,还可以转换成表示树。(表达式目录树)

用来表示Lambda表达式逻辑的一种数据结构,它将代码表达成为一个对象树,而非可执行的代码。

需要compile()才可以执行。

例子:

 class Program
    {
        static void Main(string[] args)
        {
            ParameterExpression a = Expression.Parameter(typeof(int), "a");
            ParameterExpression b = Expression.Parameter(typeof(int), "b");

            BinaryExpression be = Expression.Add(a, b);
            //构造表示式树
            Expression<Func<int, int, int>> expTree = Expression.Lambda<Func<int, int, int>>
                (be, a, b);
            //分析树的结构
            BinaryExpression body = (BinaryExpression)expTree.Body;
            //左右节点
            ParameterExpression left = (ParameterExpression)body.Left;
            ParameterExpression right = (ParameterExpression)body.Right;

            Console.WriteLine("表达式树的结构为:" + expTree);
            //
            Console.WriteLine(expTree.Body);

            Console.WriteLine(expTree.Body);

            Console.WriteLine("左树{0}{4}结点类型{1}{4}",
                left.Name,left.Type,right.Name,right.Type,Environment.NewLine);

            //
            Expression<Func<int, int, int>> expTree2 = (c,d) => c + d;
            // compile才能转换代码的执行
            Func<int, int, int>delinstatnce = expTree2.Compile();
            int resule = delinstatnce(10, 10);
            Console.WriteLine(expTree.Body);
            Console.ReadKey();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值