How do I break a loop at a certain point when debugging?
-
06-07-2019 - |
Question
Env: Visual Studio 2008 - C#
I have a for
which runs for 1000+ times over a string array.
I want to have my app break when one of the strings matches a certain term so I can walk through at that point in my code.
Now I know I can add a piece of code that looks for this and a break point when it hits, but is there not a way to do this in the debugger?
Solution
Go to your code
- create a breakpoint
- right click on the red dot on the left
- select condition
- put something like i == 1000
or
at the middle of your loop
write
if (i == 1000){
int a = 1;
}
and break over int a = 1;
The second method looks more like garbage, but I find it easier and faster to do
OTHER TIPS
In visual studio you can set a conditional breakpoint - set a breakpoint at the point where you want to break as normal and then right click on the brown circle in the left margin and select "conditional breakpoint..." or whatever. You then type in an expression that evaluates to true when you want to break (e.g. i == 1000, or MyString = "hello world")