Is it possible to do that in C#?
Queue<string> helperStrings = {"right", "left", "up", "down"};
or do I have to produce an array first for that?
No you cannot initialize a queue in that way.
Anyway, you can do something like this:
var q = new Queue<string>( new[]{ "A", "B", "C" });
and this, obviously, means to pass through an array.
Is it possible to do that in C#?
Unfortunately no.
The rule for collection initializers in C# is that the object must (1) implement IEnumerable, and (2) have an Add method. The collection initializer
new C(q) { r, s, t }
is rewritten as
temp = new C(q);
temp.Add(r);
temp.Add(s);
temp.Add(t);
and then results in whatever is in temp.
Queue<T>
implements IEnumerable but it does not have an Add method; it has an Enqueue method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With