我可以使用 lambda 语法忽略委托参数吗?
我很好奇为什么 C# 允许我在某些情况下忽略委托参数,但在其他情况下不允许。
例如,这是允许的:
Action<int> action = delegate { Console.WriteLine("delegate"); };
但这不是:
Action<int> action = () => Console.WriteLine("lambda");
有没有办法初始化委托并使用 lambda 忽略参数? 我知道我可以向 lambda 添加一个参数并修复前一行,但这更多是一个与编译器以及其工作原理或原理相关的学术问题。
I am curious why C# allows me to ignore delegate parameters in some cases but not others.
For instance this is permitted:
Action<int> action = delegate { Console.WriteLine("delegate"); };
but this is not:
Action<int> action = () => Console.WriteLine("lambda");
Is there a way to initialize a delegate and ignore the parameters using a lambda? I know that I can add a single parameter to the lambda and fix the previous line but this is more of an academic question pertaining to the compiler and why or how this works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
详细说明 tvanfosson 的答案; C# 3.0 语言规范 (§7.14) 中描述了此行为:
我认为:
相当于:
也无法编译。 正如 Daniel Plaisted 所说, () 明确表示没有任何参数。
如果有一个等价的 delegate{} ,它可能是:
这不是很漂亮,我怀疑它不符合 lambda 表达式的精神。
To elaborate on tvanfosson's answer; this behavior is described in the C# 3.0 language specification (§7.14):
I think:
is the equivalent of:
which wouldn't compile either. As Daniel Plaisted says () is explicitly saying there aren't any parameters.
If there were an equivalent of delegate{} it might be:
Which isn't very pretty and I suspect it suspect isn't in the spirit of lambda expressions.
正如其他人所说,不,您不能跳过将参数声明为 lambda。 但是,为了干净起见,我建议给它们起一个名称,例如 _。 例如,
您本身并没有忽略它们,而是表明您不关心它们是什么并且不会使用它们。
As others said, no, you can't skip declaring the parameters to a lambda. But, for cleanliness, I suggest giving them a name such as _. For example
You aren't ignoring them per-se, but you're indicating you don't care what they are and will not use them.
()=>; ... 语法明确指定 lambda 不带参数。 也许可以修改语言,以便 () =>; 真正的意思是“为我推断这个 lambda 的参数”,就像委托语法一样,但这会让语言变得更加复杂。 设计新的语言功能时,从负 100 开始,我认为这个没有通过测试。
还可能有更多的技术原因导致这难以实现(这可能更符合您的要求,但我怀疑技术原因是否推动了这一决定)。
The () => ... syntax explicitly specifies that the lambda takes no parameters. Perhaps the language could be modified so that () => really meant "Infer the parameters of this lambda for me" in the same way the delegate syntax does, but that would make the language more complicated. When designing new language features, you start at minus 100, and I don't think this one passes the test.
There may also be more technical reasons why this would be difficult to implement (which is probably more in line with what you were asking for, but I doubt the technical reasons drove this decision if it ever came up).
我想说的是强制使用 lambda 表达式的参数。
以您的第一个示例为例,您将如何与传入的值进行交互,它没有本地表示。
I'd say it's to have a forced use of the parameters of the lambda expression.
Take your first example, how would you interact with the passed in value, there's no local representation of it.
那这个呢?
What about this?
实际上, delegate {} 不指定任何参数并且适合任何委托方法签名 - 因此它在您的第一个构造中是允许的。
Lambda 表达式 () => ...; 特别声明无参数委托,这与 Action 所需的签名相矛盾 - 具有单个参数的委托。
您可能想要使用以下选项之一。
如果您需要该操作具有参数,您可以采用下一种方式(“_”是标识符名称的合法字符)。
或者您可能想使用无参数 Action,如下所示:
Actually, delegate {} does not specify any parameters and fits any delegate method signature - therefore it is permitted in your first construcion.
The Lambda expression () => ...; specifically states parameterless delegate, which contradicts the signature required by Action - a delegate with single parameter.
You may want to use one of the following options.
If you need the action to have a parameter, you can do it the next way ("_" is a legal character for identifier name).
Or you may want to use parameterless Action as follows:
我相信您的第一个示例实际上创建了一个匿名函数,该函数能够采用许多不同的签名,其主体是单个语句
Console.WriteLine...
。 因为它可以匹配不同的签名,所以不会造成问题。 在第二个示例中,lambda 语法本身定义了一个函数,该函数不采用具有相同主体的参数。 显然后者与定义的 Action 不一致,所以你会得到错误。C# 匿名方法参考
I believe that your first sample actually creates an anonymous function that is able to take on many different signatures whose body is the single statement
Console.WriteLine...
. Because it can match different signatures, it does not cause a problem. In the second sample, the lambda syntax itself defines a function that takes no parameters with the same body. Obviously the latter is not consistent with the defined Action so you get the error.C# Anonymous Method Reference