繁体   English   中英

为“ in”参数获取“无法在匿名方法,lambda表达式或查询表达式中使用ref或out参数”

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM