Fundamentals: Deep Cloning In C#
To make an object cloneable in .NET we should implement ICloneable interface. ICloneable is a simple interface with only one method Clone(). In this post I will show you how to make a deep clone of an object.
class Program
{
static void Main(string[] args)
{
Customer c1 = new Customer
{
FirstName = "John",
LastName = "Smith",
Limit = new CreditLimit
{
AccountOverdue = false,
ApprovedAmount = 1000
} };
Customer c2 = (Customer)c1.Clone();
c2.FirstName = "Deepak";
c2.LastName = "Kapoor";
c2.Limit.AccountOverdue = true;
c2.Limit.ApprovedAmount = 5000;
Console.WriteLine("C1.FirstName = {0}, C2.FirstName = {1}", c1.FirstName, c2.FirstName);
Console.WriteLine("C1.LastName = {0}, C2.LastName = {1}", c1.LastName, c2.LastName);
Console.WriteLine("C1.Limit.AccountOverdue = {0}, C2.Limit.AccountOverdue = {1}",
c1.Limit.AccountOverdue, c2.Limit.AccountOverdue);
Console.WriteLine("C1.Limit.ApprovedAmount = {0}, C2.Limit.ApprovedAmount = {1}",
c1.Limit.ApprovedAmount, c2.Limit.ApprovedAmount);
Console.ReadKey();
}
}
[Serializable]
public class Customer : ICloneable
{
public string FirstName { get; set; }
public string LastName { get; set; }
public CreditLimit Limit { get; set; }
#region ICloneable Members
public object Clone()
{
object clone;
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
// Serialize this object
formatter.Serialize(stream, this);
stream.Position = 0;
// Deserialize to another object
clone = formatter.Deserialize(stream);
}
return clone;
}
#endregion
}
[Serializable]
public class CreditLimit
{
public int ApprovedAmount { get; set; }
public bool AccountOverdue { get; set; }
}
In the code above I have a Customer class which has three properties. In the clone method I use a MemoryStream to serialize and then de-serialize to another object. One of the properties of customer class is called Limit which is of type CreditLimit. I have done this to illustrated deep cloning. As a test I write property values of the original customer class and its clone to console. Note that I modify values for the cloned object. That’s all you need to do for cloning. Nice and simple.
Tagged with: C#
2 Responses to Fundamentals: Deep Cloning In C#
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


[...] public links >> serialize Fundamentals: Deep Cloning In C# First saved by suzytino | 0 days ago Serialize And Unserialize With PHP First saved by [...]
I have a class that inherit another class from a dll that i don’t have Permission to change it and it’s not marked as [Serializable].
I need to clone an object of this class what should i do?