ConcurrentBagbag = new ConcurrentBag (); bag.Add(1); bag.Add(2); bag.Add(3);
ConcurrentBagIn this example, a ConcurrentBag of integers is instantiated and a list of integers is generated using Enumerable.Range(). The Parallel.ForEach() method is then used to add each integer to the bag concurrently. Package library: System.Collections.Concurrentbag = new ConcurrentBag (); List list = Enumerable.Range(1, 100).ToList(); Parallel.ForEach(list, (i) => { bag.Add(i); });