lambda表达式和事件处理程序? [英] lambda expression and event handler?

查看:110
本文介绍了lambda表达式和事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人向我提到c#支持使用lambda表达式作为事件处理程序,任何人都可以和我分享一些参考吗?

Someone mentioned to me that c# supports to use lambda expression as event handler, can anyone share with me some reference on this?

首选代码段。 / p>

A code snippet is preferred.

推荐答案

您可以使用lambda表达式构建匿名方法,该方法可以附加到事件上。

You can use a lambda expression to build an anonymous method, which can be attached to an event.

例如,如果您制作一个带有按钮标签的Windows窗体,您可以在构造函数中添加(在 InitializeComponent()之后):

For example, if you make a Windows Form with a Button and a Label, you could add, in the constructor (after InitializeComponent()):

 this.button1.Click += (o,e) =>
     {
        this.label1.Text = "You clicked the button!";
     };

这将导致标签随着单击按钮而更改。

This will cause the label to change as the button is clicked.

这篇关于lambda表达式和事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆