Ste*_*unn 4 c# uwp c#-7.2 in-parameters
...特别是in(readonly ref)参数.这是我的情况:
我在同一个Visual Studio解决方案中有一个UWP项目和一个UWP单元测试项目.两个项目都以C#7.2为目标.主要的UWP项目有这个类(注意in参数):
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}";
}
Run Code Online (Sandbox Code Playgroud)
当我使用UWP测试项目中的那些操作符时:
[TestMethod]
public void TestMethod1()
{
Cell cell1 = new Cell(0, 0);
Cell cell2 = new Cell(1, 1);
var added = cell1 + cell2 ;
var minus = cell1 - cell2 ;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
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'
Run Code Online (Sandbox Code Playgroud)
但是,主UWP项目中的相同用法不会产生任何编译器错误.
这是为什么?
in运算符中存在编译器错误,从另一个项目/程序集加载运算符时会丢失.
https://github.com/dotnet/roslyn/pull/23508(修复程序将在15.6预览3中发布)
https://github.com/dotnet/roslyn/issues/23689(本期的另一份报告)