Is there a way to add multiple items to ConcurrentBag all at once, instead of one at a time? I don\'t see an AddRange() method on ConcurrentBag, but there is a Concat(). How
The Concat
method is an approach contained at public Enumerable static class that support System.Linq library (inner .NET System.Core assembly).
BUT, the Concat
contains limits to provide the "add range" requirement to ConcurrentBag
To Concat
method matches the "add range" requirement, it's necessary to renew the current ConcurrentBag
Then, I do not use Concat
approach and if I may do a recommendation, I DO NOT RECOMMEND. I follow a similar example from Eric's answer, where I developed a derived class from ConcurrentBag
public class ConcurrentBagCompleted
: ConcurrentBag { public ConcurrentBagCompleted() : base() { } public ConcurrentBagCompleted(IEnumerable collection):base(collection) { } public void AddRange(IEnumerable collection) { Parallel.ForEach(collection, item => { base.Add(item); }); } }