Is there a fancy way to know you are in the last loop in a List without using counters
List<string> myList = new List<string>() {"are", "we", "there", "yet"};
foreach(string myString in myList) {
// Is there a fancy way to find out here if this is the last time through?
}
No, you will have to use a regular for(int i=0; i<myList.Length; i++) { ... }
loop for that.
How about using a for loop instead?
List<string> myList = new List<string>() {"are", "we", "there", "yet"};
for (var i=0; i<myList.Count; i++)
{
var myString = myList[i];
if (i==myList.Count-1)
{
// this is the last item in the list
}
}
foreach
is useful - but if you need to keep count of what you are doing, or index things by count, then just revert to a good old for
loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With