-
February 6th, 2011, 10:39 AM
#1
How to copy one element of a Generic Collection?
I need to copy one element of a generic collection and add it to the list. Something similar to this:
private List<CalculationResult> cantileverResults = new List<CalculationResult>();
cantileverResults.Add(cantileverResults[previousIndex]);
The problem with this solution is that when I modify the new element, the previousIndex element changes as well. I believe this is because they are reference-type, not value-type. How can I just copy (clone?) the information from one element to another without affecting each other any further?
-
February 6th, 2011, 01:24 PM
#2
Re: How to copy one element of a Generic Collection?
One approach is to write a copy constructor
http://msdn.microsoft.com/en-us/libr...16(VS.80).aspx
Then, use it.
Code:
private List<CalculationResult> cantileverResults = new List<CalculationResult>();
cantileverResults.Add( new CalculationResult ( cantileverResults[previousIndex] ) );
Another approach is to implement ICloneable
http://msdn.microsoft.com/en-us/libr...cloneable.aspx
Code:
private List<CalculationResult> cantileverResults = new List<CalculationResult>();
cantileverResults.Add( cantileverResults[previousIndex].Clone( ) as CalculationResult ) );
-
February 6th, 2011, 03:28 PM
#3
Re: How to copy one element of a Generic Collection?
Thank you. Good answer. At the end there's an extra paranthesis, but the solution works fine.
-
February 7th, 2011, 02:52 AM
#4
Re: How to copy one element of a Generic Collection?
There is some dispute about whether IClonable is an advisable interface to implement. Here is a sampling of arguments for and against. Mostly it boils down to IClonable not specifying whether a deep or a shallow copy is to be made (which some regard as bad).
Against: http://blogs.msdn.com/b/brada/archiv...03/125427.aspx
For: http://channel9.msdn.com/Forums/Tech...-best-practise
I'm not suggesting either is correct, but I was influenced by this when I was looking into this only a few days ago and wanted to bring it to wider attention.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
February 7th, 2011, 02:56 AM
#5
Re: How to copy one element of a Generic Collection?
Originally Posted by Arjay
Code:
cantileverResults.Add( cantileverResults[previousIndex].Clone( ) as CalculationResult ) );
Actually, another question. I note you use the 'as' keyword instead of just casting it. I typically do not use the 'as' keyword for any purpose. Is there a reason you preferred it here over a cast? (The difference appearing to be behavior on conversion failure; exception with a cast, return null with 'as'). If there is a performance benefit, that would be useful information. Just curious.
Thanks.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
February 7th, 2011, 05:51 AM
#6
Re: How to copy one element of a Generic Collection?
in this I think it would be preferable to use a C style cast if only because it makes things blow up if the object is of the wrong type, and since you expect the type to be correct at all times in this case, it is better to crash instantly than in the future with a NullReferenceException.
Originally Posted by BioPhysEngr
I typically do not use the 'as' keyword for any purpose.
The 'as' keyword is best for cases in which the cast may fail, but you expect it to happen occasionally and it is not a bug in your program. It compiles to a single CLR instruction, so even though it is probably slightly less efficient it isn't going to be a bottleneck. There is a good discussion of the implementation at Eric Lippert's blog.
Last edited by BigEd781; February 7th, 2011 at 05:54 AM.
-
February 7th, 2011, 12:53 PM
#7
Re: How to copy one element of a Generic Collection?
Let's not over think this.
Actually I wouldn't use the ICloneable interface at all because I would prefer to return a typesafe object. That way I wouldn't need to use a cast or 'as'.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|