为什么不能在lambda表达式中使用ref或out参数?
我今天遇到了错误并找到了解决方法,但我仍然很好奇为什么这是编译时错误.
CS1628:不能在匿名方法,lambda表达式或查询表达式中的ref或out参数'parameter'中使用
这是一个简单的例子:
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();
}
Run Code Online (Sandbox Code Playgroud) 我可以将带有out参数的方法作为Func传递吗?
public IList<Foo> FindForBar(string bar, out int count) { }
// somewhere else
public IList<T> Find(Func<string, int, List<T>> listFunction) { }
Run Code Online (Sandbox Code Playgroud)
Func需要一个类型,因此不会在那里编译,并且调用listFunction需要一个int并且不允许输入.
有没有办法做到这一点?