Jimmy Bogard
Jimmy Bogard

Reputation: 26775

Getting "Cannot use ref or out parameter inside an anonymous method, lambda expression, or query expression" for an "in" parameter

C# 7.2 added readonly structs and the in modifier for method parameters. However, you get a compiler error when you try to use these structs with reference-like semantics in lambda expressions:

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);
}

Compiling returns an error:

error CS1628: Cannot use ref or out parameter 'toMatch' inside an anonymous method, lambda expression, or query expression.

It's not a ref or out parameter, however.

Upvotes: 2

Views: 2104

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063599

Behind the scenes, in is a ref parameter, but with fancy semantics. The same as out is a ref parameter with fancy semantics. The compiler message could be clearer, perhaps - that might be a good bug to log on the Roslyn github. But: it is correct to error. I would agree that the error should explicitly mention in parameters.

Upvotes: 4

Related Questions