object assignment

前端 未结 7 1060
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 07:07

I have a scenario like:

MyClass obj1 = new MyClass();
............//some operations on obj1;
MyClass obj2 = new MyClass();
obj2 = obj1;

I h

7条回答
  •  情话喂你
    2020-12-21 07:42

    Assuming that the types on the object are simple, could you simply write a function that performs a kind of MemberwiseClone e.g.

    MyClass obj = new MyClass();
    // do your thing
    MyClass objCopy = new MyClass();
    objCopy.IamInt = obj.IamInt;
    objCopy.IamString = obj.IamString;
    

    Also more generally I found this Jon Skeet article very helpful when considering referencing.

提交回复
热议问题