3

If a class implemented ICloneable, what does that mean?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 3
    Unfortunately, not much. http://pro-thoughts.blogspot.com/2009/02/write-deep-clone-forget-about.html – Dan Bryant Nov 28 '10 at 14:39
  • You can get a little explanation here too..http://www.devx.com/vb2themax/Tip/18707 – Karthik Ratnam Nov 28 '10 at 14:43
  • Please note that this is a legal implementation of `Clone`: `class Foo : ICloneable { public int Value { get; private set; } public Foo(int value) { this.Value = value; } public object Clone() { return "Hello, World!"; } }` Interfaces say nothing about behavior, they only tell you about the existence of a method with a given signature and return type. The _intent_ is that the method return a clone (either shallow or deep) or the object, but it does not _promise_ that. – jason Nov 28 '10 at 14:51

4 Answers4

6

That is has the IClonable.Clone method. The documentation says that the method is intended to clone objects. The documentation notes specifically that the clone can be either deep or shallow. It is also noted the the resulting type must be of the same type as the object that is cloned, but there is no guarantee in the type system that it actually is so.

To sum it up, it does not offer much hard promises, but the intent is to create independent clones.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
0

Basically, it just allows the class to be cloned:

http://msdn.microsoft.com/en-us/library/system.icloneable.aspx

When implementing any interface, you are required to define the methods in that interface. In this case, the Clone method will need to be defined in your class.

Example from Microsoft:

public object Clone()
{
    return this.MemberwiseClone();
}
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • No, it only says that there is a method named `Clone` accepting no parameters and returning an instance of `object`; it says nothing about behavior. – jason Nov 28 '10 at 14:49
  • When a class implements an interface, it is the class's responsibility to define the methods in that interface. – Evan Mulawski Nov 28 '10 at 14:54
0

It just means that the class must implement a method Clone that returns an object, not more than that. So you can have a method that accepts an ICloneable and then you can clone that object.

Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • Just because a class has a method named `Clone` does not mean that that method clones instances of the class when invoked. An interface is not a promise about behavior, it is only a promise about the existence of a method with a given signature and return type on the class. – jason Nov 28 '10 at 14:48
  • The last five words of your second sentence are "then you can clone that object." Notice my first sentence "Just because a class has a method named `Clone` does not mean that that method clones instances of the class when invoked." Only one of us can be right; either `ICloneable` means you can clone that object or it doesn't. I argue that it only means that you can invoke a method named `Clone` on the object, but there is no guarantee that it will produce a clone. cf. my comment to the OP. – jason Dec 04 '10 at 00:46
  • @Jason Yeah, I understand what you mean. My first sentence covers what the interface promises, and the second sentence covers what should be expected if implemented properly. Perhaps that wasn't clear – Oskar Kjellin Dec 05 '10 at 17:06
0

ICloneable is not meaningful by itself, but may be useful in conjunction with other constraints (e.g. one could provide that a parameter must be a Foo that implements ICloneable). Thus, one could have a Foo, a CloneableFoo, an AdvancedFoo, and a CloneableAdvancedFoo, allowing Foo derivatives that support cloning to be distinguished from those that do not, but also allowing routines that expect a cloneable Foo to accept a cloneable derivative of Foo.

Unfortunately, while a function parameter passed with IClonable and Foo constraints may be used as an IClonable and as a Foo, without typecasts, there's no way to create a field meeting such criteria, nor is there any way to typecast a field. A remedy for this may be to create an ICloneable(Of T As ICloneable(Of T)), which contains a "Clone" method that returns T, and a "Self" method which also returns T (Thus, a field holding an "ICloneable Of Foo" could be accessed as a Foo via the "Self" method). A little care would be needed to make this all work, but the pattern should be quite nice.

supercat
  • 77,689
  • 9
  • 166
  • 211