它是一个很好的做法,定义一个空的委托身体的活动吗? [英] Is it a good practice to define an empty delegate body for a event?

查看:114
本文介绍了它是一个很好的做法,定义一个空的委托身体的活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
是否有缺点的事件声明添加匿名空委托?






它是一个很好的做法,以定义一个空的委托身体的事件,这样你就不用担心提高它们没有什么事件处理事件? (不需要检查事件是否为null)



像下面的代码:

 公共事件的EventHandler< LoadEventArgs> LoadedData =代表{}; 


解决方案

我当然认为它是有用的,是的。将会有一个很小,很小的性能代价 - 但不必执行无效测试可读性的利益使得它值得IMO



值得指出的是,这是当它是很好的使用匿名方法,而不是lambda表达式的几次之一 - 否则,你的名字,你会忽略的参数,如:

 公共事件的EventHandler< LoadEventArgs> LoadedData =(发件人,参数)=> {}; 



我不喜欢的名字的东西,我不打算用:)


Possible Duplicate:
Is there a downside to adding an anonymous empty delegate on event declaration?

Is it a good practice to define an empty delegate body for a event so that you do not need to worry raise a event which have no event handler? ( no need to check whether event is null).

Like code below:

public event EventHandler<LoadEventArgs> LoadedData = delegate { };

解决方案

I've certainly found it useful, yes. There will be a tiny, tiny performance cost - but the benefit in readability for not having to perform the nullity test makes it worth it IMO.

It's worth pointing out that this is one of the few times when it's good to use an anonymous method rather than a lambda expression - otherwise you have to name the parameters that you're going to ignore, like this:

public event EventHandler<LoadEventArgs> LoadedData = (sender, args) => {};

I don't like having to name things I'm not intending to use :)

这篇关于它是一个很好的做法,定义一个空的委托身体的活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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