How to specify a literal initialiser for Stack or Queue?
Collection initializers are a compiler feature that call the Add
method with each item you pass.
If there is no Add
method, you can't use it.
Instead, you can call the Stack
or Queue
constructor that takes an IEnumerable<T>
:
var stack = new Stack<int>(new [] { 1, 2, 3 });