使用lambda进行winforms事件有什么问题吗? [英] Is there anything wrong with using lambda for winforms event?

查看:64
本文介绍了使用lambda进行winforms事件有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题.我之所以问是因为我从未见过它,这让我想知道是否出了什么问题.

This is a very simple question. I'm asking because I've never seen it before which makes me wonder if there's something wrong.

comboBox1.MouseEnter += (a, b) => comboBox1.Focus();
campaignDataGridView.MouseEnter += (a, b) => campaignDataGridView.Focus();

推荐答案

这是完全可以接受的,但是,由于这些是匿名委托,因此无法取消订阅事件处理程序.

This is perfectly acceptable, however, since these are anonymous delegates, there is no way to unsubscribe the event handler.

也就是说:

// Subscribe lambda as event handler
comboBox1.MouseEnter += (a, b) => comboBox1.Focus(); 

// Try to unsubscribe a _different_ lambda with identical syntax. 
// The first lambda is still subscribed
comboBox1.MouseEnter -= (a, b) => comboBox1.Focus(); 

是否有问题取决于您的应用程序和使用情况.

Whether that is a problem or not depends on your application and use.

这篇关于使用lambda进行winforms事件有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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