How to make a copy of a collection?

A

Andrew

Hello, friends,

I need to make a copy of a collection of user defined class, which contains
DataTable and other data type properties.

However, I don't want to iterate each item. Any better way?

Thanks a lot!
 
J

Jon Skeet [C# MVP]

Andrew said:
I need to make a copy of a collection of user defined class, which contains
DataTable and other data type properties.

However, I don't want to iterate each item. Any better way?

Well, two obvious questions:

1) Does the copy need to be deep? If so, it's going to be tricky
2) Why don't you want to iterate over each item?

Also, what kind of collection are we talking about? That could make a
significant difference.
 
P

Peter Duniho

Hello, friends,

I need to make a copy of a collection of user defined class, which
contains
DataTable and other data type properties.

However, I don't want to iterate each item. Any better way?

It depends on what collection class you're using. But, for example, if
it's a class that can be instantiated using an array of the original
objects, and it implements ICollection/ICollection<T> (and why wouldn't
it? :) ), you could use the ICollection.ToArray() method to get an array,
and then use that array to create a new instance of the collection.

This has the disadvantage that you wind up copying the data in the
collection twice. But it's easy to write, and as long as copying the list
isn't a performance bottleneck, I wouldn't worry about the double copy.

Alternatively, if you're using .NET 3.5, LINQ offers the ToList()
extension method. If your collection is actually a List<T>, then that'd
be a direct method of creating a new copy of the List<T> straight from the
original. It seems to me that the method exists mainly to support LINQ's
database access features, but if you can use it, why not? :)

Note of course that even using these techniques, _some_ code has to
iterate each item. You may be able to avoid writing the code explicitly
to do so, depending on the circumstance, but you can't avoid having the
code actually do it. This is probably obvious, but I figure I should
mention it anyway. :)

Pete
 
A

Andrew

(1) The copy could be deep. One property could be an object, say another user
defined class, although majority of properties are simple data types.
(2) The iteration would be lengthy, and is easy to introduce errors.

They are collections of user defined class. A user defined class could also
inherit another user defined class. However, I believe all properties are all
serializable, including the DataTable type property.
 
A

Andrew

What about this one? It borrowed the idea I googled.

public static object CopyCollection(object graph)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, graph);
memoryStream.Position = 0;
return binaryFormatter.Deserialize(memoryStream);
}
 
P

Peter Duniho

What about this one? It borrowed the idea I googled.

public static object CopyCollection(object graph)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, graph);
memoryStream.Position = 0;
return binaryFormatter.Deserialize(memoryStream);
}

If everything in your collection is serializable, it seems reasonable to
me. It's probably going to be one of the slowest ways to copy the
collection, but if that's not a concern and it does exactly what you want,
I don't see anything wrong with it per se.

Do keep in mind that just because a class is serializable, that doesn't
mean that by serializing and deseralizing it you get an exact duplicate of
what you started with. Usually serializing/deserializing will duplicate
the important parts and that may be exactly what you want. But there
definitely can be some subtle differences between doing a true clone and
just serializing/deserializing the data.

Pete
 
J

Jon Skeet [C# MVP]

Andrew said:
(1) The copy could be deep. One property could be an object, say another user
defined class, although majority of properties are simple data types.

If you need to make a deep copy, you're in for a world of pain to be
honest.
(2) The iteration would be lengthy, and is easy to introduce errors.

If you're copying a collection, iteration will be involved *somewhere*,
even if it's at a very low level.
They are collections of user defined class. A user defined class could also
inherit another user defined class. However, I believe all properties are all
serializable, including the DataTable type property.

Yes, you've said that they're collections of a user defined class - but
what's the collection itself? A List<T>, a Dictionary<K,V>? Something
else entirely? Or is the collection a user defined collection as well?
 
J

Jon Skeet [C# MVP]

Andrew said:
What about this one? It borrowed the idea I googled.

public static object CopyCollection(object graph)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, graph);
memoryStream.Position = 0;
return binaryFormatter.Deserialize(memoryStream);
}

I'm somewhat surprised that you believe iterating over a collection
will be slow and risky, but think that binary serialization is okay.

It's almost certainly going to be significantly slower, and it's easy
to introduce errors in serialization...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top