Why use the <T> stack instead of the List <T>?
List<T> from System.Collections.Generic does everything Stack<T> and much more - they are based on the same basic data structure. Under what conditions is it more correct to choose Stack<T> instead?
You would use the stack if you needed a collection of Last In First Out items. The list will allow you to access them in any index. There are many other differences, but I would say that this is the most fundamental.
Update after your comment:
I would say that using Stack<T> makes an expression about how you want to use this code. It’s always good to plan for the future, but if you have a need for Stack<T> right now and there is no good reason to use List<T> , then I would go with Stack<T>
Well, you would like to use Stack if you logically tried to present a stack. It will convey the programmer’s intention throughout the code if you use the stack, and this will prevent the data structure from being used incorrectly by advertisements (inadvertently adding / removing / reading somewhere other than one end).
Of course, it is possible that Stack , and not a specific implementation, may just be an interface. Then you could use something like List implement this interface. The problem is mainly about convenience. If someone needs a stack, they need to select a specific implementation and remember ("Oh yes, a list of the preferred implementation of the stack"), and not just create a specific type.
why am I artificially confining myself to using the stack in new code
Here is your answer - you should use Stack when you need to implement the contractual expectation that the data structure used can only work as a stack. Of course, the time you really want to make is limited, but it is an important tool when appropriate.
For example, it is assumed that the data you are working with makes no sense unless the stack order is set. In these cases, you would run into problems if you made the data available as a list. Using Stack (or Queue , or any other order-sensitive structure) you can specify in your code exactly how the data is supposed to be used.
All about the concept. A list is a list and a stack is a stack, and they do two different things. Their only commonality is their general character and their variable length.
A list is a set of variable-length elements in which any element can be accessed and overwritten by an index, and to which elements you can add and from which elements you can remove any such index.
A Stack is a set of variable length elements that supports the LIFO access model; only the top element of the stack can be accessed, and elements can only be added and removed from this “endpoint” of the collection. Elements of element 3 from the "top" can only be accessed by "popping up" two elements above it to expose it.
Use the right tool for the job; use List when you need "random" access to any item in the collection. Use Stack if you want to provide more limited access to the elements in the array, accessible only "top-only". Use the queue when you want to enforce the FIFO pipeline; items come in one end, the other.
System.Collections.Generic.Stack<T> is a LIFO (Last-In, First-Out) aka stack data structure.
Despite its name, SCG.List<T> is not an abstract data type, known as a [linked] list : it is, in fact, an array of variable length .
Two very different creatures.