Reputation: 26775
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
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