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?
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).
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.
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.
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 04:54 AM.
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'.
Bookmarks