delegate{}和(输入参数)的区别=> {}
我有一些这样的方法
public void DoSomething(Action<int> action) { ... }
在某些情况下我不使用传递到操作中的参数。我应该注意这样称呼它
DoSomething(delegate { ... });
或
DoSomething(_ => { ... });
I have a few methods like this
public void DoSomething(Action<int> action) { ... }
In some cases I do not use the parameters passed into action. Are there any differences I should be aware of between calling it like this
DoSomething(delegate { ... });
or
DoSomething(_ => { ... });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,它们是等价的。就我个人而言,我更喜欢
delegate {}
因为很明显您不关心参数(甚至不命名它们),并且您不需要根据委托调整代码签名 - 但两者都很好。No, they're equivalent. Personally I prefer
delegate {}
as it's obvious that you don't care about the parameters (to the extent of not even naming them), and you don't need to adapt the code based on the delegate signature - but both are fine.