Implement IClonable Interface in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 3826 red flag

How to Implement IClonable interface in C# class

Implement IClonable Interface in C# Class.


In this article we will see how to implement IClonable interface in our own C# class.  IClonable interface contains only one method called clone(). The signature of Clone() method is like below.

Object Clone().

The return type of Clone() function is object, means that it can return any object including object of user’s defined class. The resulting clone must be of the same type, or compatible with the original instance.

Let’s implement IClonable interface in our C# class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
 
namespace Asynchronious
{
 
    class Person : ICloneable
    {
        public string Name { get; set; }
        public string Surname { get; set; }
       
        //Overloaded function
        public Person Clone(Person tmp)
        {
            return (Person)tmp.MemberwiseClone();
        }
        
        //Implementation of IClonable Interface
        public object Clone()
        {
            return (Person)MemberwiseClone();
        }
    }
   
    class Program
        {
            public static void Main(String[] args)
            {
                Person p = new Person();
                p.Name = "Sourav ";
                p.Surname = "Kayal";
 
                Person p1 = new Person();
                p1 = p.Clone(p);
 
                Console.WriteLine("Call to Overloaded Function");
                Console.WriteLine(p1.Name);
                Console.WriteLine(p1.Surname);
 
                Console.WriteLine("Call to Clone Function");
                Person p2 =(Person) p.Clone();
                Console.WriteLine(p2.Name);
                Console.WriteLine(p2.Surname);
               
                Console.ReadLine();
            }
        }
}

In this example we have implemented Clone() method and one of it’s overloaded method. Normally Clone() method does not take any argument. The overloaded method of Clone() can able to take one object of Person class.

Here is sample output.


An implementation of Clone can perform either deep copy or shallow copy. In deep copy, all objects are duplicated and in shallow copy, only the top-level objects are duplicated and the lower levels contain references.

Let’s implement another example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
 
namespace Asynchronious
{
 
    class Person : ICloneable
    {
        List<Person> List = new List<Person>();
        public string Name { get; set; }
        public string Surname { get; set; }
        public Person() { }
        public Person(List<Person> tmp)
        {
            foreach(Person p in tmp)
            {
                List.Add((Person)p.Clone());
            }
        }
 
        public object Clone()
        {
            return (Person) this.MemberwiseClone();
        }
 
        public void ShowResullt()
        {
            foreach (Person O in List)
            {
                Console.WriteLine("Name:- " + O.Name + "  Surname:- "+  O.Surname);
            }
        }
 
    }
   
    class Program
        {
            public static void Main(String[] args)
            {
                List<Person> List = new List<Person>();
                Person p = new Person();
                p.Name = "Sourav";
                p.Surname = "Kayal";
                List.Add(p);
 
                p = new Person();
                p.Name = "Ram";
                p.Surname = "Das";
                List.Add(p);
 
                Person p1 = new Person(List);
                p1.ShowResullt();
                Console.ReadLine();
            }
        }
}

Here is sample output.


Conclusion :- 

In this article we have seen how to implement IClonable interface in our own C# class. Hope, you have understood the concept.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)