有这样的情况
struct Point
{
int x;
int y;
public void SetX(int a){ ... }
public void SetY(int a){ ... }
}
class A
{
Point cord = new Point();
public Point Cord
{
get { return cord; }
}
public void MethodA(int a)
{
cord.SetX(a); //здесь все ок
}
}
class B
{
A myObj = new A();
public Point Cord
{
get { return cord; }
}
public void MethodB(int a)
{
myObj.Cord.SetX(a); //а здесь не присваивает значение
}
}
在 A 类中,该方法可以正常工作,但在 B 类中不能正常工作(它进入方法,在那里它为某物赋值,但 myObj 对象及其 cord 字段中没有 Point 类型)我想知道这个值是什么分配给以及为什么它适用于类,但不适用于结构。
因为该结构是 ValueType 并且被完全复制。
作为调用的结果
将有一个新的结构对象,您可以在其中调用
SetX.