Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Cloning an ArrayList (C#)

I want to clone an ArrayList such that the elements that are not simple types are also cloned..  A "deep copy" as .NET calls it.. 

Is there a way to do it w/o iterating over the list?

Elliot

RaZ
Tuesday, July 20, 2004

Hi,
Did u try using ArrayList.Clone() ? .Is it not solving your purpose?

MM
Wednesday, July 21, 2004

As the documentation clearly states, Clone does a shallow copy, not a deep copy.

There is no built-in general deep copy functionality anywhere in .NET, mostly because it's unclear how a deep copy of arbitrary types should be performed.

There was some MSDN blogging about this subject a while back... the CLR designers now say that ICloneable was misdesigned because there's no distinction between shallow and deep copy.

You could, of course, get CodeSmith and download the current version of my Collection Templates which do have a deep copy feature. You'll still have to provide a deep copy version of Clone (or another method) for all your element types, though.

Chris Nahr
Wednesday, July 21, 2004

All my types that are within the ArrayList() have an overloaded method Clone(), therefore, as I see it, Clone() should automagically call those object's Clone(). 

This is annoying.

Guess I'll just write my own.

RAz
Wednesday, July 21, 2004

*  Recent Topics

*  Fog Creek Home