Possible to iterate backwards through a foreach?

前端 未结 11 2164
无人及你
无人及你 2020-11-27 03:54

I know I could use a for statement and achieve the same effect, but can I loop backwards through a foreach loop in C#?

11条回答
  •  遥遥无期
    2020-11-27 04:42

    Before using foreach for iteration, reverse the list by the reverse method:

        myList.Reverse();
        foreach( List listItem in myList)
        {
           Console.WriteLine(listItem);
        }
    

提交回复
热议问题