Which list to choose for Parallel.For ? For example, in the collection of 1000 items. I want each thread to take 10 elements. What collection to use for this?

 List<string> lst = new List<string>(); // В lst 1000 данных Parallel.For(0, lst.Count, i) => { }); 

    1 answer 1

    If the question is how to ensure that each stream takes a certain number of elements in a row, then for this there is the following standard approach:

     Parallel.ForEach(Partitioner.Create(0, lst.Count, 10), range => { for (int i = range.Item1; i < range.Item2; i++) { // Используем элементы // lst[i] } }); 

    The Partitioner.Create method splits the sequence into ranges. The used overload specifies the size of the range (10).

    A collection can be any one that implements the IList interface so that you can access elements by index.