我有以下C#代码。
SomeObject myObject = new SomeObject();
SomeObject anotherObject = new SomeObject();
anotherObject = myObject;
如果我修改myObject,则anotherObject也会被修改。 有什么办法可以解除它们的绑定? 像这样:
Object.unbind(myObject, anotherObject);
这样修改myObject不会影响anotherObject吗?
答案 0 :(得分:1)
您可能知道 class 是 CallByReference ,并且您对第二个对象所做的任何操作都会反映在第一个对象上反之亦然。因此,克隆它,或者只是创建一个空的Second对象并逐一设置属性。
答案 1 :(得分:0)
只需将变量分配给其他对象,或将其分配为null:
int n = 13;
vector<int> a;
for(int i = 0; i < n; i++)
a.push_back(i); //! Insert needed values instead of i if required
cout<<"vector a length is "<<a.size();
或
anotherObject = new SomeObject();