How would one do a deep copy in .NET?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

can any one find and tell the difference between dot net and php which one is best ? which one we get more salary? which one is stable and which one is best for freshers and also better in future and carrer ? which one we wil get more salary sir ? please send ur valuable suggestions to kiranpulsar2007@gmail.com

1474


What r the asp.net list controls and diff. Between them?

493


Explain significance of routing? : asp.net mvc

544


In how many ways we can retrieve table records count?

530


How should I destroy my objects in asp.net?

595






How long should a session id be?

520


How to implement role based security in asp.net mvc? : Asp.Net MVC

431


What are the different types of cookies in asp.net?

516


What is a reflection?

577


Explain difference betn dataset and recordset?

522


What are validator? Name the validation controls in asp.net? How do you disable them?

538


what is publisher?

1644


what cut off mark for po's,what questions they asked for interview?

1415


What is Web API Routing?

598


Explain the updatepanel?

578