4

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.

2
  • You can define conditions in break-point
    – Roar S.
    Sep 2, 2020 at 19:23
  • Your for there is a foreach actually. You should use a for and just change the starting index and voila
    – Franck
    Sep 2, 2020 at 19:24

1 Answer 1

9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.