Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How would one do a deep copy in .NET?

Answers were Sorted based on User's Feedback



How would one do a deep copy in .NET?..

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

How would one do a deep copy in .NET?..

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

How would one do a deep copy in .NET?..

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

How would one do a deep copy in .NET?..

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

How would one do a deep copy in .NET?..

Answer / sanjay

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

How would one do a deep copy in .NET?..

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

Post New Answer

More ASP.NET Interview Questions

Describe paging in asp.net?

0 Answers  


Which validator control you use if you need to make sure the values in two different controls matched?

0 Answers  


What does mvc represent in asp.net? : asp.net mvc

0 Answers  


What is difference between asp state management and asp.net state management?

0 Answers  


Give an example of cookie abuse.

0 Answers  


What are the three parts of an http response?

0 Answers  


What is the difference between client-side and server-side validations in ASP.NET?

0 Answers   Sans Pareil IT Services,


What is Boxing and Unboxing?

7 Answers   Accenture, IBM, Tech Mahindra,


Can private assembly be used for more than one application? If so how?

7 Answers   Satyam,


explain about Back ground process control in .net

1 Answers   Digital Mesh, Infronics,


What symbol would you use to denote, the start of a code block in aspx views?

0 Answers   NA,


What is the Difference between Web.config and global.asax? Can we write connection String code in global.asax?

10 Answers   Microsystems,


Categories