... in particular, in
(readonly ref
) parameters. Here's my situation:
I have a UWP project and a UWP Unit Test project in the same Visual Studio solution. Both projects target C# 7.2 The main UWP project has this class (note the in
parameters):
public struct Cell
{
public Cell(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
public static Cell operator +(in Cell left, in Cell right)
{
return new Cell(left.X + right.X, left.Y + right.Y);
}
public static Cell operator -(in Cell left, in Cell right)
{
return new Cell(left.X - right.X, left.Y - right.Y);
}
public override string ToString() => $"{X}, {Y}";
}
When I use those operators from the UWP test project:
[TestMethod]
public void TestMethod1()
{
Cell cell1 = new Cell(0, 0);
Cell cell2 = new Cell(1, 1);
var added = cell1 + cell2 ;
var minus = cell1 - cell2 ;
}
I get:
UnitTest.cs(16,25,16,38): error CS0019: Operator '+' cannot be applied to operands of type 'Cell' and 'Cell'
UnitTest.cs(17,25,17,38): error CS0019: Operator '-' cannot be applied to operands of type 'Cell' and 'Cell'
However, the same usage inside the main UWP project does not produce any compiler errors.
Why is that?
There is a compiler bug with in
in operators, which gets lost when loading the operator from another project/assembly.
https://github.com/dotnet/roslyn/pull/23508 (the fix will ship in 15.6 preview 3)
https://github.com/dotnet/roslyn/issues/23689 (another report of this issue)