Why is an out parameter not allowed within an anonymous method?

anonymous-methods, c#, out-parameters, ref-parameters

Solution

In some ways this is a dupe. `Out` parameters are `ref` 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 as `ref` 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 the `out` parameter mean then?

Problem

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

Original source

Related problems