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

前端 未结 26 931
迷失自我
迷失自我 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:25

    What about little simpler approach.

    Item last = null;
    foreach (Item result in Model.Results)
    {
        // do something with each item
    
        last = result;
    }
    
    //Here Item 'last' contains the last object that came in the last of foreach loop.
    DoSomethingOnLastElement(last);
    

提交回复
热议问题