如何使用多维数组调用 Parallel.ForEach

发布于 2024-09-10 16:50:18 字数 646 浏览 4 评论 0原文

我在弄清楚如何使用 2D 字符串数组调用 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"}};

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

如果我没有明确指定类型,则会收到以下错误:

方法的类型参数 'System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable, System.Action)' 不能 从使用情况推断。尝试 指定类型参数 明确地。

显式指定类型参数的正确方法是什么?

I'm having a little trouble figuring out how to call the Parallel.ForEach with a 2D 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 don't specify the type explicitly I get the following error:

The type arguments for method
'System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable,
System.Action)' cannot be
inferred from the usage. Try
specifying the type arguments
explicitly.

What's the proper way to specify the type arguments explicitly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ偶尔ゞ 2024-09-17 16:50:18

您面临的问题是二维数组没有实现 IEnumerable<一维数组>。 (它确实实现了 IEnumerable,但它是一个“扁平化”数组的字符串的 IEnumerable。)您可以执行两件事:

  • 更改 string[ ,] 为锯齿状数组,string[][]

  • 实现您自己的扩展方法,迭代二维数组并将其转换为 IEnumerable<一维数组>

The problem for you is that 2-dimensional arrays do not implement IEnumerable<one-dimensional-array>. (It does implement IEnumerable, but it's an IEnumerable of strings that "flattens" the array.) You can do two things:

  • Change the string[,] to a jagged array-of-arrays, string[][].

  • Implement your own extension method that iterates over a two-dimensional array and turns it into an IEnumerable<one-dimensional-array>.

私藏温柔 2024-09-17 16:50:18

您仍然应该能够使用多维数组来完成此操作,只需使用 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
        }
    }
);

You should still be able to make this work with a multi-dimensional array, just using Parallel.For instead of 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
        }
    }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文