Search code examples
c#foreachbehaviorblockingqueueblockingcollection

Does foreach remove from C# BlockingCollection?


does anyone know if, when iterating on a C# BlockingCollection<>, the elements are taken from the collection, in the same way that BlockingCollection.Take() does 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 for some reason got BlockingQueue in my head and used that.


Solution

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

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