Create a Deep Copy in C#

It is completely impossible to deep-copy an arbitrary object. For example, how would you handle a Control or a FileStream or an HttpWebResponse? Your code cannot know how the object works and what its fields are supposed to contain. Do not do this. It’s a recipe for disaster.

Shallow copy and deep copy in C

No. A shallow copy in this particular context means that you copy “references” (pointers, whatever) to objects, and the backing store of these references or pointers is identical, it’s the very same object at the same memory location. A deep copy, in contrast, means that you copy an entire object (struct). If it has members … Read more

Deep copy of dictionaries gives Analyze error in Xcode 4.2

Presumably, it is because deepCopy does not begin with the prefix copy. So you may want to change to something like copyWithDeepCopiedValues (or something like that), and then see if the analyzer flags that. Update As Alexsander noted, you can use attributes to denote reference counting intent. This should (IMO) be the exception to the … Read more

how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

A couple of years ago, I wrote a few category methods for exactly the same reason, transforming a whole tree of user defaults to mutable. Here they are – use them at your own risk! 🙂 // // SPDeepCopy.h // // Created by Sherm Pendley on 3/15/09. // #import <Cocoa/Cocoa.h> // Deep -copy and -mutableCopy … Read more

Shallow copy or Deep copy?

From the link here Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements. Deep copies duplicate everything. A deep copy of a collection is two collections with all of the … Read more

Does Object.assign() create a deep copy or a shallow copy?

Forget about deep copy, even shallow copy isn’t safe, if the object you’re copying has a property with enumerable attribute set to false. MDN : The Object.assign() method only copies enumerable and own properties from a source object to a target object take this example var o = {}; Object.defineProperty(o,’x’,{enumerable: false,value : 15}); var ob={}; … Read more

Java HashMap – deep copy

Take a look at Deep Cloning, on Google Code you can find a library. You can read it on https://github.com/kostaskougios/cloning. How it works is easy. This can clone any object, and the object doesnt have to implement any interfaces, like serializable. Cloner cloner = new Cloner(); MyClass clone = cloner.deepClone(o); // clone is a deep-clone … Read more