ICloneable.Clone Method
.NET Framework 4
Updated: October 2010
Creates a new object that is a copy of the current instance.
Assembly: mscorlib (in mscorlib.dll)
Clone can be implemented either as a deep copy or a shallow copy. In a deep copy, all objects are duplicated; whereas, in a shallow copy, only the top-level objects are duplicated and the lower levels contain references.
The resulting clone must be of the same type as or a compatible type to the original instance.
See MemberwiseClone for more information on cloning, deep vs. shallow copies, and examples.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
MS recommends against using ICloneable
I am copying this information from the community documentation for Object.Memberwise clone, as it seems rather important to the documentation of this class.
Although the ICloneable interface can be implemented to perform a deep copy operation, the interface itself does not require that that the ICloneable.Clone method perform a deep copy. Because of this, developers have been urged to avoid implementing ICloneable. For more information, see Brad Abrams' blog post at http://blogs.msdn.com/b/brada/archive/2004/05/03/125427.aspx.
--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation
Although the ICloneable interface can be implemented to perform a deep copy operation, the interface itself does not require that that the ICloneable.Clone method perform a deep copy. Because of this, developers have been urged to avoid implementing ICloneable. For more information, see Brad Abrams' blog post at http://blogs.msdn.com/b/brada/archive/2004/05/03/125427.aspx.
--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation
- 2/16/2011
- AristosQueue
ICloneable example
using System.Collections.Generic;
using System.Text;
namespace ICloneableTest
{
class Student : ICloneable
{
private string firstName;
private string lastName;
private string regNo;
public string RegNo
{
get { return regNo; }
set { this.regNo = value; }
}
public string LastName
{
get { return lastName; }
set { this.lastName = value; }
}
public string FirstName
{
get { return firstName; }
set { this.firstName = value; }
}
public Student(string _firstName, string _lastName, string _regNo)
{
this.firstName = _firstName;
this.lastName = _lastName;
this.regNo = _regNo;
}
#region ICloneable Members
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
}
}
===============================================
using System;
using System.Collections.Generic;
using System.Text;
namespace ICloneableTest
{
class Program
{
static void Main(string[] args)
{
//Create Student.
Student student1 = new Student("abc", "pqr", "xyz");
//Clone Student1.
Student student2 = (Student)student1.Clone();
//Here you can see it gives same value.
Console.WriteLine("Before we change student1s first name");
Console.WriteLine(student1.FirstName + " " + student2.FirstName);
//By changing one objects property, you can understand they are not same reference.
student1.FirstName = "ABC";
Console.WriteLine("After we changed student1s first name");
Console.WriteLine(student1.FirstName + " " + student2.FirstName);
Console.Read();
}
}
}
- 9/15/2010
- SampathDR
