Guy
Guy

Reputation: 336

Create a collection type that once will be a List<T> and in other uses will be Stack<t>

I am developing a card game. I created a card collection that I use for two cases :

  1. A specific player cards.
  2. First package.

For the first use I need that the collection type will be List. For the second use I need that the collection type will be Stack.

Is it possible to create a collection type that once will be a List and in different use it will be a Stack?

Both card collcetions use have the same functions, that's why I need this capability.

Upvotes: 2

Views: 74

Answers (1)

David Ly
David Ly

Reputation: 31596

Just inherit from List<T> and add the Push and Pop methods and you're pretty much there.

If you actually want to prevent List functionality (random access, etc) when it's in "stack mode" then you'll want to create an IStack<T> interface and use it as an IStack<T> when you only want stack functionality.

Upvotes: 1

Related Questions