This:
List<string> set = new List<string>() { "a","b" };
works fine, but:
Stack<string> set = new Stack<string>() { "a","b" };
Queue<string> set = new Queue<string>() { "a","b" };
fails with:
...does not contain a definition for 'Add'
which does make me wonder why the compiler was dumb enough to ask for Add.
So, how should one initialise at a Queue/Stack constructor?
Collection initializers are a compiler feature that call the
Addmethod with each item you pass.If there is no
Addmethod, you can’t use it.Instead, you can call the
StackorQueueconstructor that takes anIEnumerable<T>: