Foreach loop, determine which is the last iteration of the loop

前端 未结 26 916
迷失自我
迷失自我 2020-11-28 02:56

I have a foreach loop and need to execute some logic when the last item is chosen from the List, e.g.:

 foreach (Item result in Mod         


        
26条回答
  •  暖寄归人
    2020-11-28 03:17

    You could just use a for loop and there is no need to add an extra if inside the for body:

    for (int i = 0; i < Model.Results.Count - 1; i++) {
        var item = Model.Results[i];
    }
    

    The -1 in the for condition takes care of skipping the last item.

提交回复
热议问题