Setting conditional breakpoints
Conditional breakpoints are another hidden gem when it comes to debugging. These allow you to specify one or several conditions. When one of these conditions are met, the code will stop at the breakpoint. Using conditional breakpoints is really easy.
Getting ready
There is nothing you specifically need to prepare to use this recipe.
How to do it…
Add the following code to your
Program.cs
file. We are simply creating a list of integers and looping through that list:List<int> myList = new List<int>() { 1, 4, 6, 9, 11 }; foreach(int num in myList) { Console.WriteLine(num); } Console.ReadLine();
Next, place a breakpoint on the
Console.WriteLine(num)
line of code inside the loop:Right-click on the breakpoint and select Conditions… from the context menu:
You will now see that Visual Studio opens a Breakpoint Settings window. Here we specify that the breakpoint needs to be hit only when the value of
num
is9
. You can add several conditions and specify different...