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.
You can't use . Remove(element) inside a foreach (var element in X) (because it results in Collection was modified; enumeration operation may not execute.
The difference between for and foreach in C# is that for loop is used as a general purpose control structure while foreach loop is specifically used for arrays and collections. In brief, both helps to execute code repeatedly but foreach loop is more specific to arrays and collections.
ArrayList provides the remove() methods, like remove (int index) and remove (Object element), you cannot use them to remove items while iterating over ArrayList in Java because they will throw ConcurrentModificationException if called during iteration.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With