错误是
"无法将只读字段分配给(...)"
我必须返回vector,它等于另外两个向量的和(vector和vector1) ReadOnlyVector只有字段X和Y.如何初始化它们并返回新的ReadOnlyVector?
public class ReadOnlyVector
{
public readonly double X;
public readonly double Y;
public ReadOnlyVector(int x, int y)
{
X = x;
Y = y;
}
public ReadOnlyVector Add (ReadOnlyVector vector, ReadOnlyVector vector1)
{
return new ReadOnlyVector {X = vector.X + vector1.X, Y = vector.Y + vector1.Y}
}
}
答案 0 :(得分:2)
使用构造函数。您目前正在使用对象初始化程序语法。
public ReadOnlyVector Add (ReadOnlyVector vector, ReadOnlyVector vector1)
{
return new ReadOnlyVector(vector.X + vector1.X, vector.Y + vector1.Y);
}