How to call Parallel.ForEach with a multidimensional array

I have a little problem with how to call Parallel.ForEachusing a two-dimensional array of strings:

string[,] board = new string[,]{
        {"A", "B", "C", "D", "E" },
        {"F", "G", "H", "I", "J"},
        {"K", "L", "M", "N", "O"},
        {"0", "1", "2", "3", "4"}};

Parallel.ForEach(board, row =>
    {
        for (int i = 0; i < row.Length; ++i)
        {
            // find all valid sequences
        }
    });

If I do not specify the type explicitly, I get the following error:

Type arguments for the System.Threading.Tasks.Parallel.ForEach method (System.Collections.Generic.IEnumerable, System.Action) cannot be decommissioned. Try to set the type arguments explicitly.

What is the correct way to explicitly specify type arguments?

+3
source share
2 answers

, 2- IEnumerable<one-dimensional-array>. ( IEnumerable, IEnumerable , "" .) :

  • string[,] , string[][].

  • , IEnumerable<one-dimensional-array>.

+6

, Parallel.For Parallel.ForEach:

string[,] board = new string[,] {
    {"A", "B", "C", "D", "E" },
    {"F", "G", "H", "I", "J"},
    {"K", "L", "M", "N", "O"},
    {"0", "1", "2", "3", "4"}
};

int height = board.GetLength(0);
int width = board.GetLength(1);

Parallel.For(0, height, y =>
    {
        for (int x = 0; x < width; ++x)
        {
            string value = board[y, x];
            // do whatever you need to do here
        }
    }
);
+3

Source: https://habr.com/ru/post/1755415/


All Articles