C#并行foreach循环查找索引 [英] c# parallel foreach loop finding index
本文介绍了C#并行foreach循环查找索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取文本文件中的所有行,并计划显示每个行的信息.如何找到循环中每个项目的索引?
I am trying to read all lines in a text file and planning to display each line info. How can I find the index for each item inside loop?
string[] lines = File.ReadAllLines("MyFile.txt");
List<string> list_lines = new List<string>(lines);
Parallel.ForEach(list_lines, (line, index) =>
{
Console.WriteLine(index);
// Console.WriteLine(list_lines[index]);
Console.WriteLine(list_lines[0]);
});
Console.ReadLine();
推荐答案
There is another overload for Parallel.ForEach
that gives you the index.
Parallel.ForEach(list_lines, (line, state, index) =>
{
Console.WriteLine(index);
Console.WriteLine(list_lines[index]);
});
这篇关于C#并行foreach循环查找索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文