简体   繁体   English

委托{}和(输入参数)=> {}之间的区别

[英]Difference between delegate {} and (input parameters) => {}

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(_ => { ... });

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. 我个人更喜欢使用delegate {}因为很明显您根本不在乎参数(甚至不命名它们),并且您不需要根据委托签名来修改代码-两者都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM