469,282 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Implementing System.ICloneable

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[i]);
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[i]);
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 <--
Jan 15 '07 #1
6 4551
"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
Jan 15 '07 #2
hi Marc,

Marc Gravell wrote:
"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[i]);
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 <--
Jan 15 '07 #3
Stefan Hoffmann wrote:
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
Jan 15 '07 #4
"Stefan Hoffmann" <st*************@explido.dewrote in message
news:eV**************@TK2MSFTNGP02.phx.gbl...
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

--
Adam Clauss
Jan 15 '07 #5
hi Adam,

Adam Clauss wrote:
>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.
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 <--
Jan 15 '07 #6
"Stefan Hoffmann" <st*************@explido.dewrote in message
news:eY**************@TK2MSFTNGP03.phx.gbl...
hi Adam,

Adam Clauss wrote:
>>public class StraightSegmentList: NullSafeCollection, System.ICloneable
{

public StraightSegmentList Clone() -- #1
>>object System.ICloneable.Clone() -- #2
>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.
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.

--
Adam Clauss
Jan 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael D. Ober | last post: by
8 posts views Thread by hex | last post: by
7 posts views Thread by Don | last post: by
1 post views Thread by Rain | last post: by
2 posts views Thread by Nathan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.