c# parallel foreach 循环查找索引

c# parallel foreach loop finding index

我正在尝试读取文本文件中的所有行并计划显示每一行信息。如何找到循环内每个项目的索引?

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();

another overload Parallel.ForEach 给你索引。

Parallel.ForEach(list_lines, (line, state, index) =>
    {
        Console.WriteLine(index);
        Console.WriteLine(list_lines[(int)index]); // The type of the `index` is Long.
    });