How to clear the ConcurrentBag
? it don\'t have any method like Clear
or RemoveAll
...
The selected answer is kind of, well, a workaround, so I'm adding my own workaround.
My solution was to look at all the available collections in the System.Collections.Concurrent namespace to find one where it was trivial to clear all elements from the collection.
The ConcurrentStack class has a Clear() method which removes all elements from the collection. In fact, it's the only collection in the namespace (currently) that does. Yes, you have to Push(T element)
instead of Add(T element)
, but frankly that's worth the time saved.