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

£可爱£侵袭症+ 提交于 2019-12-03 11:40:00
JaredPar

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?

Noldorin

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 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.)

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.
}

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.

    protected delegate void OutStringDelegate(int divider, out string errorText);
    protected void codeWrapper(int divider, OutStringDelegate del)
    {
        string ErrorMessage = "An Error Occurred.";

        try
        {
            del(divider, out ErrorMessage);
        }
        catch
        {
            LogError(ErrorMessage);
        }
    }
    public void UseWrapper(int input)
    {
        codeWrapper(input, codeToCall);
    }
    private int somePrivateValue = 0;
    private void codeToCall(int divider, out string errorMessage)
    {
        errorMessage = "Nice Error Message here!";
        somePrivateValue = 1 / divider; // call me with zero to cause error.
    }
    private void LogError(string msg)
    {
        Console.WriteLine(msg);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!