Implementing System.ICloneable

S

Stefan Hoffmann

hi,

the following implementations work:

public class NullSafeCollection: System.Collections.CollectionBase,
System.ICloneable
{
object System.IClonable.Clone()
{
NullSafeCollection clone = new NullSafeCollection();
for (int i = 0; i < this.Count; i++)
clone.Add(this);
return clone;
}
}

public class NullSafeCollection: System.Collections.CollectionBase,
System.ICloneable
{
public object Clone()
{
NullSafeCollection clone = new NullSafeCollection();
for (int i = 0; i < this.Count; i++)
clone.Add(this);
return clone;
}
}


Why can i not use 'public object System.ICloneable.Clone()'?

So what is the difference between naming the methods
'System.IClonable.Clone()' and 'Clone()'?


mfG
--> stefan <--
 
M

Marc Gravell

"object System.IClonable.Clone() {}" is an explicit implementation of
an interface, so the visibility is implied (either public or internal)
via the interface itself.

Marc
 
S

Stefan Hoffmann

hi Marc,

Marc said:
"object System.IClonable.Clone() {}" is an explicit implementation of
an interface, so the visibility is implied (either public or internal)
via the interface itself.
Thanks, but i still don't see why this works:

public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone()
{
StraightSegmentList clone = new StraightSegmentList();
// I need a shallow copy only.
for (int i = 0; i < this.Count; i++)
clone.Add(this);
return clone;
}

object System.ICloneable.Clone()
{
return this.Clone();
}

Maybe you can enlighten me.

btw, when do i override the MemberwiseClone() method instead of
implementing System.IClonable?



mfG
--> stefan <--
 
?

=?ISO-8859-15?Q?Arne_Vajh=F8j?=

Stefan said:
btw, when do i override the MemberwiseClone() method instead of
implementing System.IClonable?

You don't override MemberwiseClone - you use it in the
Clone method you add.

Arne
 
A

Adam Clauss

Stefan Hoffmann said:
hi Marc,
public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone() -- #1
object System.ICloneable.Clone() -- #2

Consider the following code:
StraightSegmentList list = ...
list.Clone();

ICloneable obj = (ICloneable)list;
obj.Clone();


If someone has a "typed" reference to your object (like the variable 'list')
that invocation of Clone will trigger the Clone() marked #1.
On the otherhand, if someone simply has an instance of ICloneable (like the
variable 'obj'), that invocation of Clone will trigger the Clone() marked
#2.

If all you have is #1 implemented, both types of invocation would call it
(#2 is not required).

As a side note, if all you need is a shallow copy, simply do 'return
MemberwiseClone()' from your clone method.

Hope that helps
 
S

Stefan Hoffmann

hi Adam,

Adam said:
Consider the following code:
StraightSegmentList list = ...
list.Clone();

ICloneable obj = (ICloneable)list;
obj.Clone();


If someone has a "typed" reference to your object (like the variable 'list')
that invocation of Clone will trigger the Clone() marked #1.
On the otherhand, if someone simply has an instance of ICloneable (like the
variable 'obj'), that invocation of Clone will trigger the Clone() marked
#2.
Okay, this makes now sense to me.
If all you have is #1 implemented, both types of invocation would call it
(#2 is not required).
Without #2 it doesn't compile under C#/.Net 1.1 with VS2003.
As a side note, if all you need is a shallow copy, simply do 'return
MemberwiseClone()' from your clone method.
I will test that. Thanx.


mfG
--> stefan <--
 
A

Adam Clauss

Stefan Hoffmann said:
hi Adam,


Without #2 it doesn't compile under C#/.Net 1.1 with VS2003.

Ahh - didn't catch it the first time. #1 has a return value of
StraightSegmentList rather than object. Thus #1 does not implement the
method defined in the interface - you are correct in that you will need #2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top