Since ob
is a class, it is a reference type , and therefore any instance of ob
, if assigned to another variable (as happens on the line ob item = t.Where(c => c.name == "hello").First();
) will automatically copy the link to the original instance, and not copy the actual instance itself. This is a common .NET theme regarding copying objects and apart from LINQ / Lambda,
To achieve what you want, you need to either create a Shallow Copy or Deep Copy of the resulting instance from your LINQ projection.
Shallow Copy is enough for your ob
class (ShallowCopy usually copies as little as possible, while DeepCopy copies everything). A good link for the differences can be found here ).
To execute a ShallowCopy object, you can simply use MemberwiseClone
, which is a built-in method of the .NET object type inherited by all objects.
For something more substantial, you will have to implement your own DeepCopy function, but it can be relatively simple. Something similar to these implementations, as indicated here and here .
Craigtp
source share