469,646 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Generic ICloneable

I'm working with Clone() for the first time, and noticed that you have to
unbox the Clone of an object that implements ICloneable:

MyObject var1 = new MyObject(); // Where MyObject implements ICloneable
MyObject va3 = (MyObject)var1.Clone();

Does anyone know why the 2.0 framework doesn't include a Generic
implementation of the ICloneable interface?
Mar 17 '06 #1
2 18749
Nathan,

If you had a generic IClonable interface, it would be something like
this:

public interface IClonable<T>
{
public T Clone();
}

If you had this, you would have to indicate what T is on your type, like
so:

public class MyClass : IClonable<MyClass>

Now, at first glance, this seems fine, but in reality, it creates a
problem. If you want to create a clone for an object, you literally have to
know to cast your instance to IClonable<MyClass>. This kind of doesn't make
sense for IClonable, since theoretically, you could define your class like
this:

public class MyClass : IClonable<MyOtherClass>

Which in the end, doesn't make sense. Granted, IClonable doesn't give
you any guarantees that the type will be the same as the class it is
implemented on, but the generic interface version doesn't do anything to
guarantee it either.

That being said, I think the designers decided to err on the side of
caution.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
I'm working with Clone() for the first time, and noticed that you have to
unbox the Clone of an object that implements ICloneable:

MyObject var1 = new MyObject(); // Where MyObject implements
ICloneable
MyObject va3 = (MyObject)var1.Clone();

Does anyone know why the 2.0 framework doesn't include a Generic
implementation of the ICloneable interface?

Mar 17 '06 #2
I think that what you were trying to say was that if you can cast to
ICloneable<X> then the object should be an X and you can cast directly to
that and call a non-interface method.

Also the OP talked about boxing which only occurs for structs so any object
implementing ICloneable<MyStruct> would have to be a MyStruct because you
cannot derive from a struct making it even more useless than for classes
where you could at least use ICloneable<MyBaseClass> sensibly to clone an
instance of MyDerivedClass in a slightly more type safe way.

IMHO the best way to implement ICloneable is as follows:

public class X : ICloneable
{
public X Clone() { ... }
object ICloneable.Clone() { return Clone(); } // This calls the above
}

This works very well with intellisense as if you have an X it gives you the
strongly typed method not the interface method.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:On**************@TK2MSFTNGP12.phx.gbl...
Nathan,

If you had a generic IClonable interface, it would be something like
this:

public interface IClonable<T>
{
public T Clone();
}

If you had this, you would have to indicate what T is on your type,
like so:

public class MyClass : IClonable<MyClass>

Now, at first glance, this seems fine, but in reality, it creates a
problem. If you want to create a clone for an object, you literally have
to know to cast your instance to IClonable<MyClass>. This kind of doesn't
make sense for IClonable, since theoretically, you could define your class
like this:

public class MyClass : IClonable<MyOtherClass>

Which in the end, doesn't make sense. Granted, IClonable doesn't give
you any guarantees that the type will be the same as the class it is
implemented on, but the generic interface version doesn't do anything to
guarantee it either.

That being said, I think the designers decided to err on the side of
caution.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
I'm working with Clone() for the first time, and noticed that you have to
unbox the Clone of an object that implements ICloneable:

MyObject var1 = new MyObject(); // Where MyObject implements
ICloneable
MyObject va3 = (MyObject)var1.Clone();

Does anyone know why the 2.0 framework doesn't include a Generic
implementation of the ICloneable interface?


Mar 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by SimonH | last post: by
1 post views Thread by Michael D. Ober | last post: by
7 posts views Thread by Don | last post: by
5 posts views Thread by Narshe | last post: by
1 post views Thread by Rain | last post: by
6 posts views Thread by Stefan Hoffmann | last post: by
7 posts views Thread by =?Utf-8?B?Sm9lbCBNZXJr?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.