How would one do a deep copy in .NET?
Answers were Sorted based on User's Feedback
Answer / bhagyesh
class A
{
public A()
{
};
public int i;
}
class TestPerson
{
static void Main()
{
A a = new A();
A b;
a.i =5;
b=a; // not a deep copy
b.i = a.i+6;
}
}
here a.i=11 and b.i = 11 because a and b both refer same
instance of object
Deep copy would be implemented in c# using copy construction
class A
{
public A()
{
};
public A(A previouscopy)
{
i = previouscopy.i;
};
public int i;
}
class TestPerson
{
static void Main()
{
A a = new A();
a.i =5;
// Create another new object, copying a.
A b = new A(a); // Deep copy using copy constructor
b.i = a.i+6;
}
}
here a.i=5 and b.i = 11 because a and b both refer it's own
instance of object
| Is This Answer Correct ? | 12 Yes | 3 No |
Answer / rad
public static T DeepCopy<T>(T obj)
{
object result = null;
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
result = (T)formatter.Deserialize(ms);
ms.Close();
}
return (T)result;
}
| Is This Answer Correct ? | 7 Yes | 2 No |
Answer / pramod nikumbh
In C# if u want to deep allocation, u can used
serialization that is use ISerializable
for shallow copy u can use cloning, that is use Iclonable
interface
| Is This Answer Correct ? | 8 Yes | 4 No |
Answer / mario j vargas
You can perform a deep copy in C# by implementing the
ICloneable interface and manually assigning the values of
each member field in the current instance to the new
instance being returned. It is important to mention in the
implementation's documentation whether or not the copy is a
shallow or deep copy.
Bhagyesh suggested using a copy constructor. This is
something I had never seen in C#, only in C++, but the idea
sounded very compelling and I think this could be another
way to achieve a deep copy. You should check out the article
"How to: Write a Copy Constructor (C# Programming Guide)" in
the MSDN documentation.
| Is This Answer Correct ? | 4 Yes | 0 No |
I Agree clone() always gives deep copy.
Now I am just diverting from the tpoic. one of the question
asked during interview is : what is the difference b/w
Array.copy() and array.clone(). for me both gives deep copy
the only difference is array.clone() returns a object type
so you have to typecast it.
Please comment.............
| Is This Answer Correct ? | 2 Yes | 3 No |
Answer / kavita
Two or more pointers have their own Data.
class A
{
string c;
}
A a;
B b;
a=b;//deep copy
| Is This Answer Correct ? | 6 Yes | 42 No |
How can you change a Master page dynamically in which event of page life cycle?
What is the difference between visual basic and asp.net?
Explain a program using razor view engine to create a simple application? : asp.net mvc
How do you sign out from forms authentication?
How to merge 2 tables fields in DataTable in asp.net
Whats the use of @ Register directives ?
What methods are fired during the page load?
What is base class of button control in .net?
How to fetch a data from one table to another table in asp.net ?
In a webservice, need to display 10 rows from a table. Which is the best choice among datareader or dataset?
What is the answer for "Which configuration Tool your using means" ? we have to tell about IIS or .Net Framework or VSS? Give me in brief ?
Difference between abstract factory pattern and factory method pattern in .NET with example.
3 Answers ACS, IBM, Keane India Ltd,
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)