1

can you tell me if after each time I call Count, the Queue go over all the Queue and count or the Queue have a property of int Count and each adding/removing it increase/decrease?

Anyway, Is there a better way to know if the Queue is empty?

3 Answers 3

6

See the MSDN docs (1st hit in Google for "queue.count property"):

Retrieving the value of this property is an O(1) operation.

Sign up to request clarification or add additional context in comments.

3 Comments

I must of skiped it. Thank you.
Maybe not everyone knows what O(1) means?
Well, there are many, many explanations about the big-O-notation for understanding algorithm costs... so if someone doesn't know about them, do some research and learn about them. Here's a starter: Time Complexity in Wikipedia
2

The capacity of a Queue<T> is the number of elements that the Queue<T> can store. Count is the number of elements that are actually in the Queue<T>.

The capacity is always greater than or equal to Count. If Count exceeds the capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.

Retrieving the value of this property is an O(1) operation.

Taken from the source Queue<T>.Count Property on MSDN

1 Comment

Don't plagiarize, quote with source!
-1

A better way to know if a Queue is empty is to use IEnumerable's Any method.

This way you won't need to iterate over the whole collection simple to know if there's at least one item in the collection when using a Count method.

if(myQueue.Any())
{
 // Do something.
}

In the case where Count is a property there will be a minor performance gain using Count vs Any however I think that the intent of the programmer is better preserved through Any in many cases, especially when the purpose of the check is to see if any items exist or not.

6 Comments

Why "better"? Check the docs... Any() is always going to be at least a tad slower.
That Any() is faster is only true in comparison to Enumerable.Count(), and only if the class doesn't implement a fast ICollection<T>.Count. One could argue that Any() reads better than Count>0, but that's only a stylistic choice.
I say better because the intention of the programmer is clearer in many cases. In the case where Count is a property then Any is a tad slower, you are correct.
@JamieDixon: You wrote: "This way you won't need to iterate over the whole collection simple to know if there's at least one item in the collection." That's a bit misleading in the context of the question asked because for me it seems to imply that checking Count of the queue would iterate through all elements.
The question is about the Queue<T>.Count property, not the Enumerable.Count() method. Enumerable.Any will force the Queue<T> to enumerate the first element, if it exists. If the OP just wanted to know if there were any elements in the queue, queue.Count > 0 (or queue.Count == ) is faster than Enumerable.Any
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.