0

This is regarding to how to retain value of an object. I have 1 object as a "objData" which has my main result. I am storing result of objData in 1 variable.

var result = objData;

After this i am removing some properties from objData as per below.

var result1 = objData.remove(x=>x.something..);

Then if i check "result" then it is also updated because it's reference type object is there any way to retain same value for "result" if i am removing some properties during creation of "result1"?

Nikhil.Patel
  • 959
  • 9
  • 17
  • Is objData a list? – Willy David Jr Aug 22 '17 at 07:06
  • @WillyDavidJr it's object with multiple properties! – Nikhil.Patel Aug 22 '17 at 07:06
  • Can you redefine the `remove` method to return a copy of the object it's applied to? – nondestructive Aug 22 '17 at 07:07
  • Essentially you want to create a copy, check out [this](https://stackoverflow.com/questions/6569486/creating-a-copy-of-an-object-in-c-sharp) question. – Tamás Szabó Aug 22 '17 at 07:09
  • 1
    `Remove` method directly updates the source, so `result` is also affected here. To keep `result` retained, you need to write custom copy method with different reference location (possibly using `new` operator). – Tetsuya Yamamoto Aug 22 '17 at 07:11
  • @TamásSzabó i have multiple operation like result1 so is there any alternative except copy the same object because i need to do it multiple time otherwise – Nikhil.Patel Aug 22 '17 at 07:11
  • @nondestructive: That just shifts the same problem to a different location (inside the remove method), which then leads to the exact same question. It also seems wasteful to implement it in the remove method, if the remove method is being called more than once on the same object. – Flater Aug 22 '17 at 10:20
  • @Nikhil.Patel: Your last comment (or rather, your intention) doesn't make logical sense. If you want to make two _separate_ instances based on a single object, then _of course_ you have to copy it. That is inherently what "copying" means. `so is there any alternative except copy` You're basically asking if there is a way to create separate objects without needing to create separate objects. Logically, the answer to that can only be no. – Flater Aug 22 '17 at 10:23
  • I don't see how you would solve this without implementing any kind of (deep) cloning. Having the cloning in `remove` would avoid manually creating a copy every time you need to call that method. – nondestructive Aug 22 '17 at 10:23
  • 1
    @nondestructive: But in regards to creating working code that copies (which is the question), it is the same regardless of _where_ you put it. Either option is valid, based on the circumstances. If you don't need a copy for each call of the remove method (e.g. make a copy once, then remove 5 different properties on the copied object), then it's not correct to put it in the remove method. But the OP's question also hinges on _how_ to do the copy in the first place, you can see from the question that he tries to assign to a different variable but does not account for how reference types work. – Flater Aug 22 '17 at 10:27
  • @Flater Ah, I see. I didn't consider removing _multiple_ properties, for some reason. Then yes, no need to put the copying logic inside `remove`. – nondestructive Aug 22 '17 at 10:29

1 Answers1

1
var result = objData;

This does not copy the value of objData, rather it just creates a second reference to the same object. Thus any modifications to the object made through either result or objData are "seen" by both.

To make a copy depends on the type of the objects. Some objects implement a Clone method1, some implement their own method, but most often you will have to create a new instance and copy over all the state:

var result = new TheType {
  Prop1 = source.Prop1,
  Prop2 = source.Prop2,
 …
};

Of course for many types such clone makes no sense (eg. an object managing a TCP socket).


1 Whether this is a deep clone – are objects cloned recursively or just the top level – depends on the type.

Richard
  • 106,783
  • 21
  • 203
  • 265