I am debugging my program. Can I set a start object for the foreach
loop in the debug mode? For instance, I want the foreach
loop to start from the 5th element of my collection.
No, you cant. Foreach loop uses IEnumerable<T>
where T is the type of object. So unlike for loop you cannot set initial or start index for the iteration. So you will always start from the 0th location object.
Other option is to use linq as shown below.
//Add using System.Linq statement at top.
int[] numbers = new int[] { 1,2,3,4,5,6,7,8};
foreach(var num in numbers.Skip(5))
{
Console.WriteLine(num);
}
Considering you are talking about debugging I'm assuming what you want to do is to break from the 5th element on etc. If this is the case then you can use a break point specifying a hit count which will break on the nth hit and so on.
See MSDN here.
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