[英]How to use a ref parameter in a thread if "Error CS1628: Cannot use in ref or out parameter inside an anonymous method, lambda or query expression"?
[英]Getting “Cannot use ref or out parameter inside an anonymous method, lambda expression, or query expression” for an “in” parameter
C#7.2为方法参数添加了只读结构和in
修饰符。 但是,当您尝试在lambda表达式中将这些结构与类似引用的语义一起使用时,会出现编译器错误:
public readonly struct Point {
public Struct(int x, int y) {
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
}
public IEnumerable<Point> FindMatching(
this IEnumerable<Point> points,
in Point toMatch) {
return point.Where(p => p.X == point.X && p.Y == point.Y);
}
编译返回错误:
错误CS1628:无法在匿名方法,lambda表达式或查询表达式中使用ref或out参数“ toMatch”。
但是,它不是ref或out参数。
在幕后, in
是一个ref
参数,但具有花哨的语义。 与out
相同的是带有花哨语义的ref
参数。 编译器消息可能更清晰,也许是登录Roslyn github的好bug 。 但是:错误是正确的。 我同意错误应该in
参数中明确提及。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.