How can I check if a Queue is empty?

Assuming you meant System.Collections.Generic.Queue<T>

if(yourQueue.Count != 0) { /* Whatever */ }

should do the trick.


I would suggest using the Any() method, as this will not do a count on the entire queue, which will be better in terms of performance.

Queue myQueue = new Queue();
    if(myQueue.Any()){
      //queue not empty
    }

Assuming you mean Queue<T> you could just use:

if (queue.Count != 0)

But why bother? Just iterate over it anyway, and if it's empty you'll never get into the body:

Queue<string> queue = new Queue<string>();

// It's fine to use foreach...
foreach (string x in queue)
{
    // We just won't get in here...
}

Tags:

C#

Queue

Is Empty