How would one do a deep copy in .NET?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is redirecting behavior?

723


Why do we need Sessions?

755


What is the difference between page-level caching and fragment caching?

723


What is autopostback in asp net?

755


How to create multi language website in asp.net mvc? : Asp.Net MVC

677


I want to connect a system in LAN and I want to access that. Whenever I am moving a mouse in my desktop, the similar thing has to happen in another system in which I have connected. I need coding for this in C# and ASP.NET Can anyone please help me. It is very urgent. Thanks in advance. My email id is manojkumarchallagundla@gmail.com Will you please?

1686


What is session and application variable in asp net?

651


What are navigation controls? How many navigation controls are there in ASP.NET 4.0?

717


What is repository pattern in mvc.net? : asp.net mvc

710


Why should i prefer JSP over asp.net or any other web development language..??

1697


Which object is used to encapsulate the state of the client and the browser in ASP.NET?

790


I’m having some trouble with cas. How can I diagnose my problem?

791


Which ASP.NET configuration options are supported in the ASP.NET implementation on the shared web hosting platform?

846


What is razor? : asp.net mvc

751


What is xaml? Are xaml file compiled or built on runtime?

716