StackOverflow 上的几个 C# 问题询问如何使用out
或ref
参数制作匿名委托/lambda。参见,例如:
为此,您只需指定参数的类型,如下所示:
public void delegate D(out T p);
// ...
D a = (out T t) => { ... }; // Lambda syntax.
D b = delegate(out T t) { ... }; // Anonymous delegate syntax.
我很好奇的是为什么明确需要该类型。有什么特别的原因会出现这种情况吗?也就是说,从编译器/语言的角度来看,为什么不允许以下内容?
D a = (out t) => { ... }; // Lambda syntax -- implicit typing.
D b = delegate(out t) { ... }; // Anonymous delegate syntax -- implicit typing.
甚至更好,只是:
D a = (t) => { ... }; // Lambda syntax -- implicit typing and ref|out-ness.
D b = delegate(t) { ... }; // Anonymous delegate syntax -- implicit typing and ref|out-ness.