C# 在另一个数组的索引处的数组值

C# 在另一个数组的索引处的数组值,c#,arrays,C#,Arrays,我有两个数组:string[]文件和string[]注释 我使用foreach循环遍历文件数组 foreach(string file in files) { comments.SetValue(//index of file we are at) } 假设我得到一个文件,它是文件数组的索引20。我想做的是能够在同一个索引处获得comments数组的值。因此,对于文件[0],我返回注释[0]、文件[1]注释[1]等。您可以假设重复不会成为问题 foreach(string file in fi

我有两个数组:string[]文件string[]注释

我使用foreach循环遍历文件数组

foreach(string file in files)
{
comments.SetValue(//index of file we are at)
}
假设我得到一个文件,它是文件数组的索引20。我想做的是能够在同一个索引处获得comments数组的值。因此,对于文件[0],我返回注释[0]、文件[1]注释[1]等。

您可以假设重复不会成为问题

foreach(string file in files)
{
    int index = files.IndexOf(file);
    //blah blah
}
但是使用for循环可能会更容易、更有效,这样您就有了索引

   for (var i = 0; i < files.Length; i++)
for(var i=0;i
我会这样做

for (int i = 0; i < files.Length; i++) {
   string file = files[i];
   string comment = comments[i];
}
for(int i=0;i
不要使用
foreach
,而是使用
for

for (var i = 0; i < files.Length; i++)
{
  // ...
}
for(var i=0;i
只需执行以下操作:

string[] files = { "1", "2", "3" };
            string[] comments = {"a","b","c"};

            int i = 0;
            foreach (string file in files)
            {
                 files[i]  = comments[i] ;
                 i++;
            }
您可以为此使用:

foreach(var fc in files.Zip(comments, (f, c) => new { File = f, Comment = c }))
{
    Console.WriteLine(fc.File);
    Console.WriteLine(fc.Comment);
}

也许考虑使用<代码>字典<代码>。