Does foreach remove from C # BlockingCollection?

Does anyone know if, when iterating over C # BlockingCollection <>, the elements are taken from the collection, just like BlockingCollection.Take (), for example?

BlockingCollection<int> q = new BlockingCollection<int>();
[...]
foreach(int i in q)
{
    //does q still contain i?
}

thanks

EDIT: Of course, I meant BlockingCollection, but somehow got a BlockingQueue in my head and used that.

+4
source share
3 answers

The enumerator BlockingCollection<T>does NOT remove items from the collection.

However, the enumerator returned from BlockingCollection<T>.GetConsumingEnumerable()REMOVE items from the collection.

+10
source

foreachjust lists the collection. He never removes anything from any collection.

+2

.NET BlockingQueue<T>.

  • BlockingCollection<T>: , foreach GetEnumerator, IEnumerator. IEnumerator - . MSDN BlockingCollection<T> , , .

  • : , , , .

Change . In fact, since it BlockingCollectiongoes around IProducerConsumerCollection<T>, it BlockingCollectionuses the enumerator of the base (internal) collection. None of the implementations IProducerConsumerCollection<T>provided by .NET modify the collection.

0
source

All Articles