为什么匿名方法中不允许使用 out 参数?
This is not a dupe of Calling a method with ref or out parameters from an anonymous method
I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much.
what are your thoughts on this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从某些方面来说,这是一个骗局。
Out
参数是ref
参数。 C# 语言使用的值只是有一个额外的属性。不允许它们的原因与 ref 参数完全相同。这里的问题源于在匿名方法内使用匿名方法外部声明的值的影响。这样做将捕获 lambda 内的值,并在必要时任意将其生命周期延长到当前函数的生命周期之外。这与具有固定生命周期的
out
参数不兼容。例如,想象一下
out
参数引用堆栈上的局部变量。 lambda 可以在未来的任意点执行,因此可以在该堆栈帧不再有效时执行。那么out
参数意味着什么?In some ways this is a dupe.
Out
parameters areref
parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same asref
parameters.The problem here originates with the effect of using a value declared outside the anonymous method within the anonymous method. Doing so will capture the value within the lambda and out of necessity arbitrarily extend its lifetime beyond that of the current function. This is not compatible with
out
parameters which have a fixed lifetime.Imagine for instance that the
out
parameter referred to a local variable on the stack. The lambda can execute at any arbitrary point in the future and hence could execute when that stack frame was no longer valid. What would theout
parameter mean then?这基本上与匿名委托/lambda 表达式的参数是捕获变量这一事实有关,而捕获
ref
/out
变量则不然。在 C#/CLR 中没有任何意义,因为它内部需要ref
/out
fields 。另外,请注意,我将这两个关键字配对,因为它们实际上是相同的。如果您想要完整的解释,Eric Lippert 在他的博客上详细讨论了这个设计点。 (特别请参阅底部附近的段落。)
This is basically to do with the fact that parameters of an anonymous delegate/lambda expressions are captured variables, and capturing
ref
/out
variables doesn't make any sense in C#/the CLR, since it would requireref
/out
fields internally. Also, note that I pair both these keywords because they are effectively the same.If you want a complete explanation, Eric Lippert discussed this design point in detail on his blog. (See the paragraphs near the bottom in particular.)
out
和ref
参数之间的唯一区别是out
参数将应用一个[out]
标记它。就 CLR 而言,它们是同一件事。为了实现它,编译器必须生成不受支持的
ref
字段。如果您考虑一下,您就会意识到允许匿名方法使用
out
参数是没有意义的。下面的代码会做什么?
The only difference between
out
andref
parameters is that anout
parameter will have an[out]
token applied to it. They're the same thing as far as the CLR is concerned.In order to implement it, the compiler would have to generate
ref
fields, which are not supported.If you think about it, you'll realize that it makes no sense to allow an anonymous method to use an
out
parameter.What would the following code to?
我在开发一些错误处理代码时遇到了这个难题。我想将引用(输出)传递给将被记录的错误消息。这使我的匿名方法有机会执行多次检查,每次检查都根据需要设置错误消息。
我最终为匿名方法编写了一个新的包装器,其工作方式有所不同。但我认为对某人可能有价值的是,我可以简单地创建一个具有 out 参数的私有方法,并定义一个委托,然后让我的代码使用它。希望这可以帮助/启发某人。
I came across this conundrum whilst developing some error handling code. I wanted to pass a reference (out) to an error message that would get logged. This gave my anonymous methods a chance to perform multiple checks, each setting the error message as necessary.
I ended up writing a new wrapper for the anonymous method that worked differently. But what I thought might be of some value to someone, is that I could have simply made a private method that had an out parameter, and defined a delegate, and made my code use that. Hope this helps / inspires somebody.