[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

C# syntax for array of Queue

I cannot find syntax for arrays of Queue!  All variations I have tried
seem to be rejected by the compiler in VS2008.
Perhaps someone can give me an answer to this?
Currently I have made the Queues separate as shown below:

        // this form does not seem to accept an argument for initial capacity!
        public Queue<double> firstQueue0 = new Queue<double>();
        public Queue<double> firstQueue1 = new Queue<double>();
        public Queue<double> firstQueue2 = new Queue<double>();
        public Queue<double> firstQueue3 = new Queue<double>();
        public Queue<double> firstQueue4 = new Queue<double>();
        public Queue<double> firstQueue5 = new Queue<double>();
        public Queue<double> firstQueue6 = new Queue<double>();
        public Queue<double> firstQueue7 = new Queue<double>();
        public Queue<double> firstQueue8 = new Queue<double>();
        public Queue<double> firstQueue9 = new Queue<double>();

THis works for me but what I would really like is an array.

Also is Queue threadsafe?

And why does  Queue<double>() not accept an initial capacity
argument like         Queue firstQueue0 = new Queue(100); does?


- mrmox

2 Solutions
1.   public Queue<double>[] queues = new  Queue<double>[2];

2. Queue.Synchronized Method
The Queue constructor with a initial capacity parameter is only available on .NET Framework 4.  Prior versions of the framework does not have that option.
BTW, regarding @dj_alik solution, you also need to initialize it as follows:

public Queue<double>[] queues = new Queue<double>[10];

for (int i=0; i < queues.Length; i++) queues[i] = new Queue();  // in your constructor

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

OK, I believe wdosanjos gives wrong information. See
for the 2.0Queue class:

"Queue (Int32)       Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the default growth factor."

About arrays. I believe the asker has VB experience and tried to create an array using "()" brackets instead of "[]". I do this mistake quite often when switching from VB to C#...

dj_alik answered about thread safety.

@anarki_jimbel, you are right. Sorry, for the misinformation.
mrmoxAuthor Commented:
Thank you, I am now able to instantiate an array of queues.
Some of my confusion may have been in thinking
I should be able to instantiate the array and
populate it with queue objects in one step/line.
So, the part from @wdosanjos was useful to me.
I would still like to understand why Queue<double>
and Queue come from different places but I did not ask
that specifically I realize.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now