473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 19424
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.