问题:
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