473,480 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 19436
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2297
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
1
2751
by: Michael D. Ober | last post by:
In VB 2005, the ICloneable interface requires the following: Class foo Implements ICloneable Public Function Clone() as Object Implements System.ICloneable.Clone ' return new_object of type...
7
26414
by: Don | last post by:
Can anyone give me an example of implementing ICloneable to give a class I created a "Clone" method so I can make copies of objects. I have no idea where to begin with this. Thanks. - Don
5
8918
by: Narshe | last post by:
I have a custom collection class that looks like this public class CustomCollection<T> : ICollection<T>, IList<T>, IEnumerable<T>, IDisposable, ICloneable, IComparer<T> {....} I need to know...
1
2276
by: Rain | last post by:
Hi. Im a C# newbie, just want to ask how to implement the ICloneable.Clone.. Dont know how it works, would really appreciate it if someone could show a simple source sample of how to do this.....
3
16741
by: semedao | last post by:
Hi, what is the best way to perform deep copy (Clone) on class that inherit from System.Collections.Generic.Dictionary ?
6
4716
by: Stefan Hoffmann | last post by:
hi, the following implementations work: public class NullSafeCollection: System.Collections.CollectionBase, System.ICloneable { object System.IClonable.Clone() { NullSafeCollection clone =...
7
2797
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
4
2064
by: Bill McCormick | last post by:
Hello, I have a list of struct's that have generic fields: public struct Item<T1, T2: IComparable<Item<T1, T2>{ private T1 _t1; private T2 _t2; public T1 t1 { get { return _t1; }
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
574
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.