事件绑定的简化

Posted on 2015-09-09 16:18  云起  阅读(2)  评论(0编辑  收藏  举报  来源
this.button1.Click += new System.EventHandler(delegate(object sender, System.EventArgs e) { this.Close(); });
void button1_Click(object sender, EventArgs e){this.Close();}

事件的绑定一般是这样,用委托和lambda表达式,可以将之简化。

委托:

this.button1.Click += new System.EventHandler(delegate(object sender, System.EventArgs e) { this.Close(); });

lambda表达式:

this.button1.Click += (ss, arg) => { this.Close(); };


Copyright © 2024 云起
Powered by .NET 9.0 on Kubernetes