Looping through 2 Lists at once

前端 未结 7 572

I have two lists that are of the same length, is it possible to loop through these two lists at once?

I am looking for the correct syntax to do the below



        
7条回答
  •  你的背包
    2021-01-31 11:12

    You can do it explicit.

    IEnumerator ListAEnum = ListA.GetEnumerator();
    IEnumerator ListBEnum = ListB.GetEnumerator();
    
    ListBEnum.MoveNext();
    while(ListAEnum.MoveNext()==true)
    {
      itemA=ListAEnum.getCurrent();
      itemB=ListBEnum.getCurrent();
      Console.WriteLine(itemA.ToString()+","+itemB.ToString());
    }
    

    At least this (or something like this) is what the compiler does for a foreach-loop. I haven't tested it though and I guess some template parameters are missing for the enumerators.

    Just look up GetEnumerator() from List and the IEnumerator-Interface.

提交回复
热议问题