Search Unity

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

Discussion in 'Scripting' started by recon, Feb 22, 2011.

  1. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    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!
     
  2. PaulAsh

    PaulAsh

    Joined:
    Nov 29, 2010
    Posts:
    108
    If you are just looking to create a new instance of stack with the members of the old stack you can do this

     
  3. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    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 :p
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    >.>



    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<T> 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<T>: 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...)
     
    Last edited: Feb 22, 2011
  5. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Hahaha it actually worked :D

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

    squidbot

    Joined:
    Jan 4, 2011
    Posts:
    128
    I think a more efficient solution would be :

    Code (csharp):
    1.  
    2. Stack<TFoo> newStack = new Stack<TFoo>(oldStack.Reverse());
    3.  
     
  7. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    @squidbot: Yes but Stack<T> doesn't actually have a Reverse() method :/
     
  8. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    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)
     
  9. squidbot

    squidbot

    Joined:
    Jan 4, 2011
    Posts:
    128
    Sorry, yes it's an extension method and I'm using them successfully, but as FizixMan says, you need to include Linq.
     
  10. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    Sweet, yes that totally worked. Thanks again guys :)