Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda expression and event handler?

Tags:

c#

.net

c#-4.0

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

A code snippet is preferred.

like image 769
Adam Lee Avatar asked Oct 09 '12 00:10

Adam Lee


1 Answers

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

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.

like image 72
Reed Copsey Avatar answered Oct 10 '22 18:10

Reed Copsey