Results 1 to 9 of 9

Thread: ICloneable - Me Vs My Book

Threaded View

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    ICloneable - Me Vs My Book

    Hello, on my ongoing learning of Interfaces I'm now reading about the ICloneable interface, which its quite easy to understand (the basics anyway!)

    well in the book I'm reading the author gave an example of how to clone a class that implements the ICloneable interface. cloning basic class is quite simple, just return the MemberwiseClone and you get (not true) deep copy:
    Code:
    public object Clone()
            {
                return this.MemberwiseClone();
            }
    well that's only work if you don't have any references to other classes inside the class you're copy, if you do those references will be shallow copy (only the references to memory will get copied)

    what the author suggested to do is to first get shallow copy:
    Code:
    Point newPoint = (Point)this.MemberwiseClone();
    and then start to copy the reference class and fill the gaps:

    Code:
    PointDescription currentDesc = new PointDescription();
    currentDesc.PetName = this.desc.PetName;
    newPoint.desc = currentDesc;
    return newPoint;
    it looked to me like hard work if the reference class has a lot of properties, what i did instead is to implement the ICloneable interface in the reference class as well, and return its MemberwiseClone too, like so:

    Code:
     public object Clone()
            {
                Point CPoint = (Point)this.MemberwiseClone();
                PointDescription desc = new PointDescription();
                desc = (PointDescription)this.test.Clone();
                CPoint.test = test;
                return CPoint;
            }
    which work as well with a lot less work.

    so my question is basically is, is it bad idea to use the ICloneable interface like i did? is it consider bad practice ?
    is using the ICloneable reduce my app performance ?

    Thanks!
    Last edited by motil; Oct 27th, 2010 at 05:00 PM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width