【发布时间】:2018-06-13 14:32:51
【问题描述】:
...特别是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}";
}
当我使用 来自 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 ;
}
我明白了:
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'
但是,在主 UWP 项目中的相同用法不会产生任何编译器错误。
这是为什么呢?
【问题讨论】:
-
那么您在Properties > Build > Advanced Build Settings 测试项目中也选择了C# 7.2?
-
@AndrésRobinet:测试项目真的需要更高的 C# 级别来针对已编译的项目运行测试吗?
-
@Andrés Robinet,是的,两者都针对 C# 7.2
标签: c# uwp c#-7.2 in-parameters