Monday 1 October 2012

.Net Tips , C# Tips : Implement ICloneable Interface with C# Examples and VB.Net Examples

Click Here to Download ImplementICloneableInterface.zip

There is a need where you wan to clone an object for your custom requirement you need to "ICloneable" Interface and Implement Clone() Method, the inbuilt Clone() method does not fulfill custom requirement.
When you assign one value type to another, at that time a copy of the value is created. No link exists between the those two values and changes in one value will not affect the other value.
 However, when we assign one reference type to another (excluding strings), at that time a new copy of the reference type did not created. but , both reference types refer to the same object, and changes into the value of the one object are reflected in other objects.
To create a actual true copy of a reference type object, we must clone the object.
For cloning we are using ICloneable Interface.
We are using "MemberwiseClone" method for shallow copy clone.

Here is example:
In this example we take one "Books" class object and implement "ICloneable"  Interface on that. After that we create one object of "Books" class and set its property after that we create another "Books" object and set our original object by "Clone" Method. You can see in output that If we change in either property of object that object value does not affected earch others.
If you remove the ICloneable Interface on class and directly assign one object to another object and making change in one object's property It will automatically change others property.

C# Examples :
    public class Books : ICloneable
    {
        public string Title { get; set; }
        public string ISBN { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int Pages { get; set; }
        public int PublisherId { get; set; }

        public object Clone()
        {
            return MemberwiseClone();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Books objBook = new Books { Title = "ASP.NET", ISBN = "asp1", ReleaseDate = DateTime.Parse("11/11/2010"), Pages = 200, PublisherId = 1 };

        Books objTmp = (Books) objBook.Clone();
        Response.Write("<b>Original Book Object:</b> ");
        Response.Write("</br>Title : " + objBook.Title);
        Response.Write("</br>ISBN : " + objBook.ISBN);
        Response.Write("</br>ReleaseDate : " + objBook.ReleaseDate);
        Response.Write("</br>Pages : " + objBook.Pages);
        Response.Write("</br></br>");
        ////Make change in copy object
        objTmp.Title = "C#";
        Response.Write("</br><b>After Change In Copy Object Book Object Unaffected:</b>");
        Response.Write("</br>Title : " + objBook.Title);
        Response.Write("</br>ISBN : " + objBook.ISBN);
        Response.Write("</br>ReleaseDate : " + objBook.ReleaseDate);
        Response.Write("</br>Pages : " + objBook.Pages);
        Response.Write("</br></br>");

        objBook.Title = "SQL";

        ////Clone object
        
        Response.Write("</br><b>Clone Object:</b>");
        Response.Write("</br>Title : " + objTmp.Title);
        Response.Write("</br>ISBN : " + objTmp.ISBN);
        Response.Write("</br>ReleaseDate : " + objTmp.ReleaseDate);
        Response.Write("</br>Pages : " + objTmp.Pages);
        Response.Write("</br></br>");

       
    }

VB.net Examples :
    Public Class Books
        Implements ICloneable

        Public Property Title() As String
        Public Property ISBN As String
        Public Property ReleaseDate As Date
        Public Property Pages As Integer
        Public Property PublisherId As Integer

        Public Function Clone() As Object Implements ICloneable.Clone
            Return MemberwiseClone()
        End Function
    End Class

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objBook As Books = New Books With {.Title = "ASP.NET", .ISBN = "asp1", .ReleaseDate = DateTime.Parse("11/11/2010"), .Pages = 200, .PublisherId = 1}

        Dim objTmp As Books = CType(objBook.Clone(), Books)
        Response.Write("<b>Original Book Object:</b> ")
        Response.Write("</br>Title : " & objBook.Title)
        Response.Write("</br>ISBN : " & objBook.ISBN)
        Response.Write("</br>ReleaseDate : " & objBook.ReleaseDate)
        Response.Write("</br>Pages : " & objBook.Pages)
        Response.Write("</br></br>")
        '//Make change in copy object
        objTmp.Title = "C#"
        Response.Write("</br><b>After Change In Copy Object Book Object Unaffected:</b>")
        Response.Write("</br>Title : " & objBook.Title)
        Response.Write("</br>ISBN : " & objBook.ISBN)
        Response.Write("</br>ReleaseDate : " & objBook.ReleaseDate)
        Response.Write("</br>Pages : " & objBook.Pages)
        Response.Write("</br></br>")

        objBook.Title = "SQL"

        '//Clone object

        Response.Write("</br><b>Clone Object:</b>")
        Response.Write("</br>Title : " & objTmp.Title)
        Response.Write("</br>ISBN : " & objTmp.ISBN)
        Response.Write("</br>ReleaseDate : " & objTmp.ReleaseDate)
        Response.Write("</br>Pages : " & objTmp.Pages)
        Response.Write("</br></br>")

    End Sub

Output :


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



No comments:

Post a Comment