我在弄清楚如何使用二维字符串数组调用 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
}
});
如果我没有明确指定类型,我会收到以下错误:
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.
显式指定类型参数的正确方法是什么?
最佳答案
你的问题是二维数组没有实现IEnumerable<one-dimensional-array>
. (它确实实现了 IEnumerable
,但它是一个 IEnumerable
的字符串,“扁平化”了数组。)你可以做两件事:
更改
string[,]
到锯齿状的数组,string[][]
.实现您自己的扩展方法,迭代二维数组并将其转换为
IEnumerable<one-dimensional-array>
.
关于c# - 如何使用多维数组调用 Parallel.ForEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3286232/