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

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

    ".Last()" didnt work for me, so I had to do something like this:

    Dictionary iterativeDictionary = someOtherDictionary;
    var index = 0;
    iterativeDictionary.ForEach(kvp => 
        index++ == iterativeDictionary.Count ? 
            /*it's the last item */ :
            /*it's not the last item */
    );
    

提交回复
热议问题