Generic Stack<T> doesn't have a .Clone() method?

msdn is telling me that a generic Stack has a Clone method, and it’s “Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0” versions of .NET

Im using Unity version 3.2.0f4

On compilation, I get the error "Type System.Collections.Generic.Stack<PathNode>' does not contain a definition for Clone’ and no extension method `Clone’ of type ". So what am I doing wrong?

Thanks!

If you are just looking to create a new instance of stack with the members of the old stack you can do this

Oh I’ve tried that. Only problem is that since the copy constructor is inherited from the IEnumerable and the enumerator reads the elements in the same order as if they were popped from the stack which results in a reversed copy of the original.

I absolutely need it to make a exact copy of the other stack, with the elements in the same order :stuck_out_tongue:

.>

Stack newStack = new Stack(new Stack(oldStack));

<.<

.>

EDIT: Yeah, this is pretty weird. Stack for me (.NET 4.0 for Silverlight and ASP.NET) also does not have the clone method despite the MSDN stating otherwise.

EDITx2: OOOOOOoooooh
System.Collections.Generic.Stack doesn’t have the Clone method because it doesn’t implement ICloneable, but the System.Collections.Stack (non-generic) does because it implemetns ICloneable.

Stack: http://msdn.microsoft.com/en-us/library/system.collections.stack_members(VS.90).aspx
Stack: http://msdn.microsoft.com/en-us/library/06wcad5h(VS.90).aspx

Oh Microsoft…

(I’m guessing this is due to legitimate implications of using generics where they can’t guarantee proper transfer of entries for indeterminate types?)

Anyway, the method I posted (as silly as it is) should work. Hopefully your stack is relatively small 'cause it’ll be a O(2n) operation. (which isn’t THAT bad…)

Hahaha it actually worked :smile:

Not the most optimized solution maybe, but i’ll do for now. Thank you for taking your time to investigate this!

I think a more efficient solution would be :

Stack<TFoo> newStack = new Stack<TFoo>(oldStack.Reverse());

@squidbot: Yes but Stack doesn’t actually have a Reverse() method :confused:

The Reverse method is an extension method provided by Linq. So if you put a “using System.Linq;” at the top of your code file, I think it might work. (I haven’t tried using extension methods or Linq in Unity, so I’m not sure)

Sorry, yes it’s an extension method and I’m using them successfully, but as FizixMan says, you need to include Linq.

Sweet, yes that totally worked. Thanks again guys :slight_smile: