Important Interface in .NET: Work With ICloneable Interface

Welcome to the “Important Interface in C#” article series. In this series we will be talking about various useful interfaces of the .NET class library. In our previous article we talked about the IEnumerable, ICollection, IComparable, IList, ISerializable and many more interfaces, you can read them here.

In this article we will learn the concepts of the ICloneable interface in the .NET class library. The ICloneable interface is used to make a clone of an existing object. It contains only one method. The following is the location of the ICloneable interface in the .NET class library.

Namespace: System
Assembly: mscorlib.dll

The prototype of the ICloneable interface is as in the following

public interface ICloneable


The method contained in ICloneable is:

Clone: Creates new object by taking a copy of an existing object.

The ICloneable interface creates an object by taking the property of an existing object. It's not necessary to mention that two objects will be alike in nature.

It's not defined whether the Clone() method will perform a deep copy or a shallow copy. In the following example we will implement the ICloneable interface in our own class. Have a look at the following example.

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.Serialization;

using System.Text;

using System.Xml.Serialization;

using System.Globalization;

 

namespace SelfHostingWebAPI

{

    public class Person :ICloneable

    {

        public string name { getset; }

        public string surname { getset; }

 

        public Person(){}

        public Person(Person p)

        {

            this.name = p.name;

            this.surname = p.surname;

        }

 

        public object Clone()

        {

            return new Person(this);

        }

    }

   class Program

    {

        static void Main(string[] args)

        {

            Person p1 = new Person { name = "Sourav", surname = "Kayal" };

            Console.Write("Name in p1:- " + p1.name + "\n");

            Person p2 =(Person) p1.Clone();

            Console.Write("Name in p2:- " + p2.name);

            Console.ReadLine();

        }

    }

}


Here is the output of the example above. We are seeing that the p2 object is nothing but a clone of the p1 object and we are accessing both the name property of p1 and p1 objects and both are producing the same value.



Make deep copy using MemberwiseClone() method

In this example we will perform a deep copy using the MemberwiseClone() method. Here is a sample implementation.

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.Serialization;

using System.Text;

using System.Xml.Serialization;

using System.Globalization;

 

namespace SelfHostingWebAPI

{

    public class Person :ICloneable

    {

        public string name { getset; }

        public string surname { getset; }

 

        public Person(){}

        public Person(Person p)

        {

            this.name = p.name;

            this.surname = p.surname;

        }

 

        public object Clone()

        {

            return this.MemberwiseClone();

 

        }

    }

   class Program

    {

        static void Main(string[] args)

        {

            Person p1 = new Person { name = "Sourav", surname = "Kayal" };

            Console.WriteLine( p1.name + " " +p1.surname);

            Person p2 =(Person) p1.Clone();

            Console.Write(p2.name + " " + p2.surname);

            Console.ReadLine();

        }

    }

}


The following is the output of the example above.



Conclusion

In this small article we learned the concepts of the ICloneable interface in the .NET class library. I hope you have liked the small implementation. Bye. See you in the next article.

 

Next Recommended Readings