views:

225

answers:

3

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

+12  A: 

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?

JaredPar
+1 Had started typing an example similar to your last paragraph. Thanks for saving me the bother :)
Binary Worrier
Second paragraph turned the lightbulb on. Thanks @JaredPar!
Chris Ballance
But wouldn't that apply to any other kind of variable? I mean, what if I use a variable inside a lambda expression which is located in the stack, just access some field. If the lambda expressión executes when the stack is no longer valid, Wouldn't the object have already been collected? If it isn't, when would the garbage collector collect that object?
Jorge Córdoba
@Jorge, if you use a local variable within a lambda expression it will not ever be located on the stack. The closure capturing mechanism will actually remove it entirely from the method (check the output with reflector). Parameters are a little bit different in that they are just copied into the lambda at the start of the method.
JaredPar
Mmmm, I don't get it completly, I'm starting a new question with this...
Jorge Córdoba
One minor nit pick: the "outness" of a ref argument is not represented by a modopt, it's represented by an attribute. It might have been a better idea to use a modopt because then you could have two methods of the same signature that differed solely in out/ref-ness. But attributes are not considered when evaluating signature equality.
Eric Lippert
@Eric, thanks for the correction, will update shortly.
JaredPar
+5  A: 

This is basically to do with the fact that parameters of an anonymous delegate/lambda expressions are capgtured variables, and capturing ref/out variables doesn't make any sense in C#/the CLR, since it would require ref/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.)

Noldorin
+1  A: 

The only difference between out and ref parameters is that an out 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?

static Func<object, object> Mess(out object param) {
    param = "Original";
    return i => param = i;
}
static Func<object, object> MessCaller() {
    object local;
    return Mess(out local);
}
static vouid Main() {
    Console.WriteLine(MessCaller()("New"));
    //The local variable that the lambda expression writes to doesn't exist anymore.
}
SLaks
`OutAttribute` attribute is not the thing applied to `out` parameters in C#. `OutAttribute` is a custom attribute considered when marshaling stuff. `out` parameters are annotated by another metadata token (`[out] in IL`). You can verify this fact by querying the attributes of a `ParameterInfo` declared as `out` in C# code. There's no `[OutAttribute]` there.
Mehrdad Afshari
Fixed; thank you.
SLaks