Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I jump to the ith iteration of a loop while debugging in VS?

I am debugging a C# program in Visual Studio. Consider the following snippet.

var List<string> cars = {"honda","bmw","ferrari","ford","jaguar"};
foreach (string car in cars){
    purchaseCar(car);
}

I wish to debug the purchaseCar() function when it is called for car="ford". However, while debugging in Visual Studio, I will have to step over(F10) the purchaseCar() function for previous three cars until I can finally step into(F11) it. This would be really tedious if there were a larger number of previous elements.

I wish to know if there is anyway I can directly jump to the ith iteration of a loop while debugging in Visual Studio.

like image 683
Ankit Giri Avatar asked Oct 16 '25 15:10

Ankit Giri


1 Answers

Rather than thinking of it as "jumping to a particular iteration" I would suggest using a conditional break point.

Add the break-point in Visual Studio, e.g. on the purchaseCar(car) line, then right click on it and select "Conditions...". You can then set a condition, which in this case would be car == "ford". Let the debugger run, and it will only break when the condition is true.

like image 185
Jon Skeet Avatar answered Oct 19 '25 04:10

Jon Skeet