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.
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();
Console.WriteLine(num)
line of code inside the loop:num
is 9
. You can add several conditions and specify different conditions. The condition logic is really flexible:num
is 9
:The condition is evaluated on every loop. When the condition is true, the breakpoint will be hit. In the example illustrated in this recipe, the true benefit of a conditional breakpoint is somewhat lost because it is a very small list. Consider this though. You are binding a data grid. Items on the grid are given specific icons based on the status of the item. Your grid contains hundreds of items, because this is a hierarchical grid. You identify the primary ID of the item which is bound to the grid. This primary ID is then passed to other code logic to determine the status, which determines the icon displayed.
To debug and press the F10 key through hundreds of loops is not productive in any event. With conditional breakpoints, you can specify a value for the primary ID, and only break when the loop hits that value. You can then go straight to the item that is being displayed incorrectly.
3.15.137.59