How to Use Conditional Breakpoints to Debug in Visual Studio
April 24, 2024
• 1,557 views
If you're writing C# code in Visual Studio and you're still debugging with Console.WriteLine, then this video is for you! Well, let's be serious -- many of us go back to writing to the console because it's convenient... But if we got to know Visual Studio better we'd see these super handy features!
In this video, I'll guide you through how to use conditional breakpoints when debugging your code in Visual Studio. I can't promise you'll stop writing to the console, but hopefully this helps make debugging easier!
View Transcript
as software Engineers debugging our code is a critical skill that we need to be able to develop and let's be honest here if you're a beginner or even a more senior engineer you're often finding yourself writing console right line to try and help debug your programs hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video we're going to walk through some basics of using conditional breakpoints inside a visual studio like I mentioned a lot of us are just used to using console right line to get by and if you think about it your IDE is probably sitting there just like that Meme where it's like am I a joke to you right we have these other tools that are built into visual studio and other idees that let us do a better job of debugging a quick
reminder to subscribe to the channel and to check that pin comment for my courses on don't drain now let's check out the code we're going to walk through a very trivial code example just so that we can illustrate how to use the tools inside of visual studio for this this very simple example is going to assume that we have this list here called some names I've just put a handful of names in into this list you'll notice that some of them are four letters long some of them are a little longer with five characters and one in particular is only three characters we have a loop here on the screen this 4 each loop from line 15 to 21 and it's got a problem inside of it we're going to try and print the fourth character of every single name that we have in this
list if you were paying close attention it's kind of obvious but one of the names in the list right on line 7 Tim does not have enough characters to be able to print the for so if we were to go run this and I'll just go ahead and press play it's going to be a little bit disappointing and that's because it's going to throw an exception like I said this is going to be a very trivial contrived example just so that I can show you the tools for how to do this so something that we might do is we might go if you know if the output that we were aiming for wasn't already to write to the console we're trying to do some other processing with this we might try to put something like an if statement in here and then we could say
like if the name that we're looking at is less than four in length right so how many characters we got it's less than four it's going to go check to see which name that you're coming across is causing the error before it happens so you might go do some type of console right line so that you can see the information coming through maybe it don't even have the if statement right maybe you do this kind of thing and you start printing out all of the names that are coming through but a lot of us rely on Console right line so that we can go find where the problem is before it happens but what I want to show you if we do this right a lot of us debug this way right we would see in the console the name Tim only has three characters
and we'd have this aha moment like now we have to go find how we're getting Tim into our data set what we're supposed to do about that but this is like the the pretty basic way that a lot of us debug things I kind of made a joke in the beginning about this being a very beginner way to do it but like honestly it's so simple a lot of the time for us to be able to do this kind of stuff that we just go do it now where this really falls down is especially in distributed systems so if you're running a web service and to repeat the issue you're kind of relying on something happening you know once something's been deployed writing to the console maybe isn't a very effective way to try getting that information right but that aside something that we can
do instead of just putting if statements in and writing to the console is something called a conditional breakpoint and that's the whole point of this video I just wanted to show you kind of like where the logic comes from why we might want to use something like this so we know that the exception is going to be thrown on line 17 when the name is too short right Tim in this case is too short I can put a breakpoint here and if we just have a breakpoint and go run this it's going to step through all of them right so every time I press F5 it will kind of keep jumping through the list one more time and now we have an error come up right kind of helpful if you want to go debug and step through but imagine imagine you had a million
things in a list right imagine you're quering a database and pulling back a million names are you going to sit there and press F5 on your keyboard up to a million times probably not and you could go do the console right line thing right you probably don't want to print out all a million of those as well and try to scan through them but you could use an if statement or you could do a conditional breakpoint so with our breakpoint placed in the left bar here in Visual Studio I can rightclick on it and go to conditions from here we have a couple of different options condition expression hit count and filter I'm just going to use conditional expression and then I'm going to type the expression that I want to break on so this is very much like us typing an if statement into
the code right so I would say if the name. length and you'll notice as I'm typing we do get the autocomplete intell ense coming up for us which is awesome so you don't have to try and remember all the things like you get that power of the editor right in this little tool so if the name length is less than four that's when I want to break so I just pressed enter after doing that if you had more complex conditions you wanted to work with you could add another one this will end the condition for you but for now we're just going to roll with this so if the name is less than four for the length we're going to break I have to do this before the exceptions actually thrown so if I put the break point say on line 18 it would be
too late we would already hit that error so if I go run this now now we hit our break point and the condition we set was the name length has to be less than four so it should not be the first iteration of the loop it should in fact have broken once we hit Tim and if we go check what the name is you can see when I hover over this it does say Tim so the condition had to be met for this break point to get hit pretty cool that's the basics of this I just want to show you one more pretty handy thing so if I go back to the condition right so I'm going to go here I right clicked on it go to the menu conditions we also have actions that we can take instead so I had a little snippet
of code here that I saved so I'm going to put this into action here and we can print out to the console so you don't have to write console right line anymore right you don't have to go change the code to do this which is pretty handy and what I want to do is basically break once this happens right so we'll hit the condition the action that we're going to take is to write a message to the console and I should have shown you that the way that this works here right if I open up a curly brace if I start typing again we get the auto complete so it's not like you have to guess at what to do like you do get all of the Power of Visual Studio autocomplete and intellisense right in here so I'm just going to putp that back
I just wanted to demonstrate that that's what's happening the continue code execution I don't want this checked off the reason I don't want this to happen is because what we'll do is we would print to the console and then we would throw the exception what I would prefer to have happen is that we hit the breakpoint conditionally we'll write something to the console and then we can go examine what's going on so let me close this I'm going to go ahead and press play again we should again be breaking on Tim because I didn't change the condition so when I hover over we see Tim here but now if I wanted to go see what was printed to the console I guess it's going to be the debug out output not the console I'm sorry so name Tim only has three characters so we could
go check this debug output have all this other information printed for helping us and we can conditionally break the execution and stop to investigate other things if we wanted to and that's a very quick look at conditional breakpoints inside a visual studio I don't know if that's going to totally persuade you to stop using console right line in your debugging efforts I know that I still use it I know lot of other software Engineers that still rely on that sort of thing especially because it's simple enough to do in some cases however we do have plenty of advanced tools that we can use inside of things like Visual Studio or if you're using other idees they have lots of great tools too these things are being built out to make our lives easier so I do suggest you try them out get used to them
because they can enable more than just a simple console right line if you found this useful and you want to see more debugging tips inside of Visual Studio you can check out the video next thanks and I'll see you next time
Frequently Asked Questions
What are conditional breakpoints and how do they differ from regular breakpoints?
Conditional breakpoints allow me to specify a condition that must be met for the breakpoint to trigger, unlike regular breakpoints that pause execution every time they are hit. This is particularly useful when I'm debugging loops or large datasets, as it helps me focus on specific cases that are causing issues.
How can I set a conditional expression for a breakpoint in Visual Studio?
To set a conditional expression, I right-click on the breakpoint in the left margin of Visual Studio, select 'Conditions', and then enter my desired condition. For example, I might check if the length of a name is less than four characters.
Can I perform actions when a conditional breakpoint is hit?
Yes, I can perform actions when a conditional breakpoint is hit. In the conditions menu, I can specify actions like writing a message to the console without modifying my code. This allows me to log useful information while still pausing execution for further investigation.
These FAQs were generated by AI from the video transcript.