Search code examples
c#collections.net-2.0

stack collection missing shift and unshift in C# 2.0


Bizarrely the stack collection seems to be missing the rather basic shift and unshift methods* and I'm working in 2.0 so I can't just extend them.

Is there any reasonable technique or alternative collection class to get these methods available? I need push and pop as well.

Edit: looks like the collection I want is indeed a deque which is happily not native to C# :(

Can't use third party libraries at this time so I'll be going with the clunky LinkedList (I say clunky because reading and removing are two operations where shift would be one) but I think I'd recommend the PowerCollections approach to anyone who could use it. Or better yet, upgrading to extension methods.

sigh


* Apologies, I didn't realise these were uncommon terms, I thought I just didn't know where to find them in the API. For reference:

shift = remove first element

unshift = insert element at beginning of collection


Solution

  • I would say use a LinkedList<T>. It has methods for adding and removing from the front, as well as adding and removing from the back. I've never heard of shifting and unshifting, but I'm assuming that's what it means.