Visual Studio Debugging - How To Set a Breakpoint Condition
Do you iterate through long for loops or while loops? Tired of pressing F8 or F5 just until you meet a certain condition while debugging in Visual Studio? I didn't know that this was possible (haven't tried in Eclipse) until a colleague told me How To Set a Breakpoint Condition in Visual Studio.
How To Set a Breakpoint Condition in Visual Studio
Let's assume that you have the following set of code... The code below does nothing but to print names from an array of string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using System;
using System.Collections.Generic;
using System.Text;
namespace BreakpointIfTest
{
class Program
{
private static string[] names = { "Tim", "Gerald", "Keith", "Cruizer", "Rolvin", "Jared", "Chris", "Jops", "Devpinoy" };
static void Main(string[] args)
{
foreach (string s in names)
{
Console.WriteLine(s);
}
}
}
} |
Suppose we set a break point at the line... Console.WriteLine(s);

and we want the breakpoint only to HIT when the value of the variable "s" is "Keith". What you do is right click on the breakpoint (the red circle) and choose "condition".

A window will pop up prompting for a breakpoint condition, enter s == "Keith"

Now run and debug your application and you will see that the breakpoint will only hit if the value of "s" is Keith". ;)

Tell me if it helps you. I hope this saves you some time debugging!