ICloneable Interface
.NET Framework 4
Supports cloning, which creates a new instance of a class with the same value as an existing instance.
Assembly: mscorlib (in mscorlib.dll)
The ICloneable type exposes the following members.
The ICloneable interface contains one member, Clone, which is intended to support cloning beyond that supplied by MemberwiseClone. For more information about cloning, deep versus shallow copies, and examples, see the Object.MemberwiseClone method.
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.
CRTP?
$0"Clone [is] intended to support cloning beyond that supplied by MemberwiseClone."$0
$0Then why isn't it using CRTP like IEquatable< > and others?$0
$0$0
$0
$0I could have understood if it was merely to use MemberwiseClone "publicly", but it explicitly specified that it is intended to provide support for more extensive cloning purposes. It would then be more appropriate to return the type being cloned rather than object because it should return an instance of that type anyway and it would make the calling code more readable since there would not be any casting involved in a majority of cases.$0
$0$0
$0
$0$0
$0
ICloneable example
using System;
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();
}
}
}
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