Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Usage of ICloneable

Usage of ICloneable

Scheduled Pinned Locked Moved C#
helpquestiondiscussion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Rohde
    wrote on last edited by
    #1

    Hi everyone, this is not really a programming question, but as I made some thoughts about cloning my objects I had an idea and just want to hear some comments on it. Normally when a class should be cloneable it implements ICloneable like this:

    public class TestClass : ICloneable {
    public object Clone() {
    //Do something
    }
    }

    The nasty thing about this is that everybody who calls Clone will have to do a cast afterwards (assuming the caller wants to do something with the result where object doesn't help). Do avoid this one could just forget ICloneable and make a typed Clone method:

    public class TestClass {
    public TestClass Clone() {
    //Do something
    }
    }

    Sadly this isn't an option for me because at some places in my code I have an ArrayList which holds objects of several different classes which I all have to clone. So I came to the idea to instead combine the two methods and do the following:

    public class TestClass : ICloneable {
    object ICloneable.Clone() {
    return this.Clone();
    }

    public TestClass Clone() {
    //Do something
    }
    }

    So now when I use a variable of type TestClass the Clone method will return TestClass, but I can use it also as an ICloneable where I get an object (Truly also having the type TestClass but unknown to the caller). So what are your thoughts about it? I think this casting problem when using Clone is relatively widespread, but why is everybody using (I refer to the framework and some 3rd party components) just the naiv method? PS: Why is the result of ICloneable.Clone an object and not ICloneable? I thought the intention of this interface was that the result should be some kind of deep copy of the object and should always have the same type... :confused: -- modified at 15:56 Sunday 9th October, 2005

    S 1 Reply Last reply
    0
    • R Robert Rohde

      Hi everyone, this is not really a programming question, but as I made some thoughts about cloning my objects I had an idea and just want to hear some comments on it. Normally when a class should be cloneable it implements ICloneable like this:

      public class TestClass : ICloneable {
      public object Clone() {
      //Do something
      }
      }

      The nasty thing about this is that everybody who calls Clone will have to do a cast afterwards (assuming the caller wants to do something with the result where object doesn't help). Do avoid this one could just forget ICloneable and make a typed Clone method:

      public class TestClass {
      public TestClass Clone() {
      //Do something
      }
      }

      Sadly this isn't an option for me because at some places in my code I have an ArrayList which holds objects of several different classes which I all have to clone. So I came to the idea to instead combine the two methods and do the following:

      public class TestClass : ICloneable {
      object ICloneable.Clone() {
      return this.Clone();
      }

      public TestClass Clone() {
      //Do something
      }
      }

      So now when I use a variable of type TestClass the Clone method will return TestClass, but I can use it also as an ICloneable where I get an object (Truly also having the type TestClass but unknown to the caller). So what are your thoughts about it? I think this casting problem when using Clone is relatively widespread, but why is everybody using (I refer to the framework and some 3rd party components) just the naiv method? PS: Why is the result of ICloneable.Clone an object and not ICloneable? I thought the intention of this interface was that the result should be some kind of deep copy of the object and should always have the same type... :confused: -- modified at 15:56 Sunday 9th October, 2005

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      If only C# supported Covariant[^] return types, then your problem would go away. You'll be able to return TestClass and still implement ICloneable.Clone. Robert Rohde wrote: Why is the result of ICloneable.Clone an object and not ICloneable? I thought the intention of this interface was that the result should be some kind of deep copy of the object and should always have the same type.. ICloneable.Clone clones an object, so why should it return ICloneable? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups