1 out of 3 rated this helpful Rate this topic

ICloneable.Clone Method

Updated: October 2010

Creates a new object that is a copy of the current instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Object Clone()

Return Value

Type: System.Object
A new object that is a copy of this instance.

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.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Date

History

Reason

October 2010

Added link to MemberwiseClone.

Customer feedback.

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
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
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();
        }
    }
}
c++
to implement this in c++ you have to use:

virtual

System::Object^ Clone();