不能在 lambda 表達式中使用 ref 或 out 參數 - Cannot use ref or out parameter in lambda expressions

問題:

Why can't you use a ref or out parameter in a lambda expression?爲什麼不能在 lambda 表達式中使用 ref 或 out 參數?

I came across the error today and found a workaround but I was still curious why this is a compile-time error.我今天遇到了這個錯誤並找到了一個解決方法,但我仍然很好奇爲什麼這是一個編譯時錯誤。

CS1628 : Cannot use in ref or out parameter 'parameter' inside an anonymous method, lambda expression, or query expression CS1628 :不能在匿名方法、lambda 表達式或查詢表達式中使用 in ref 或 out 參數“parameter”

Here's a simple example:這是一個簡單的例子:

private void Foo()
{
    int value;
    Bar(out value);
}

private void Bar(out int value)
{
    value = 3;
    int[] array = { 1, 2, 3, 4, 5 };
    int newValue = array.Where(a => a == value).First();
}

解決方案:

參考一: https://en.stackoom.com/question/5jHF
參考二: https://stackoom.com/question/5jHF
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章