C# Debugging: Breakpoints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in perfect world your c-sharp application is going to work right for the first time and if that happens to you every single time you know what this video probably isn't for you but for the rest of us let's look at some techniques to quickly identify where the problem is so we can fix it now here I have an application I built in this application is just a demo application it's not meant to be an actual full-fledged application in fact this application doesn't even work if I hit run here I get this nasty exception now this takes me right to the part of the code whereas exception happens but let's first figure out why this exception is happening as you can see I'm getting a system dot argument out of range exception now we're gonna pretend for a minute we don't know why that's happening I want to create an ultra complex example where it's hard to spot the error so those of you who are watching home you may look at this and say oh I see the error I know how to fix it but that's not really the point here the point here is we don't know we're gonna pretend we don't know why this is happening and so how we fix this well first thing we can do is we know the error happens here on line 26 inside of our person class so we can use what's called a break point to stop the operation somewhere in here before we get to that point so we can look at the data and identify what's happening and walk through the code so let's do that now if you want look at this function see what's actually doing we're taking the first name property we're doing we're just saying hey every other character make it an uppercase letter it's really funky and weird but that's what we wanted to do this right here this line right here line 26 is our problem but we don't know why in fact line 26 isn't actually our problem every time but what we can do is say well if line 26 is the problem I can click over here in this gray gutter over here and notice this red spot that appears and it also highlights my line in this maroon color and what this indicates is that we had a breakpoint here now a job of a breakpoint is to stop the code at this particular instance risk particular spot and allow us to identify what's going on right now kind of like pausing time you know we hit the pause button at this point and we can look at where everything is as it is frozen in time and then we can actually start walking through and looking at what happens next and why it happens so let's just look at first of all what a breakpoint does by hey if start button okay you notice the screen flashed really quickly on and then it went right to this line of code and we're now highlighted in yellow and if you can see over here on the left in that little circle is now a yellow arrow that indicates that we have paused our application at this line and now we can look at all the different variables as they are in this moment in time so if I mouse over my first name property you can see that first name equals Timothy that's a little small but there you go first name equals Timothy okay so that's the value right now of our first name property now we can almost look at the I variable I is currently one and if we look at output output has just a capital T and that's it at this point we know what's going on we're not quite sure why we're getting that error that didn't really help us that much it looks like the variables are right and in fact first name a substring of first name at position 1 1 character means it's going to look for the I in Timothy because it's 0 1 0 is T 1 is I that looks right so now we can do is we have three options and if you look up here you can see those three options step into step over and step out we're going to primarily use the step into f11 but once in a while use f10 now get into what those do in a minute but let's first look at the step into and what this does this is f11 or this icon right here step into says take one step in the application that's it so do one thing and then stop again so in this case it completed this one line and then it stopped at our end curly brace and now we can actually look at output again and see output now has T I because that I was uppercase and then added to our output so that worked line 26 didn't blow up but we're in a for loop so it may blow up next time now we can do here is a couple different things one we could hit f11 and step through line by line over and over again the other option though is to hit the continue button right up here or f5 and that will bios did that that will allow the co to continue running as if it were normally running but then notice it's back here in this line and the reason why is we still have our break point here and so our break point says hope you hit that line so go ahead and pause and now if we look I is 3 instead of 1 which means that we've run this for loop twice not just the once B this if right here it says I modulus 2 equals 0 well this right here a percent sign is called modulus and what means is it tells us the remainder of this division so for example 2 divided by 2 there's a 0 is a remainder therefore this would be true if I was to I 3 so 3 modulus 2 means 2 goes into 3 one time fully and that's it as a remainder of 1 therefore this we false and we go down to the else statement so basically what this does is these are evens and these are odds okay so we come down here we say well I is 3 and first name has at least three characters therefore we're good so actually it's 4 characters because we're a base 0 and that's gonna be something that comes up in a minute so let's go ahead and we hit f11 and step into one line and then look at our output and our output now it says t IM o which is great the other thing you do is just hit f5 if we wanted to and I hit f5 or the continue button up here and now I've gone to the next time we hit this breakpoint in this case I is 5 instead of 3 okay it continued again and now I is 7 and our output says if you notice our output says Timothy and that's where we get our first clue that something's not right because our output has our entire name in there if you notice the first name variable or property its Timothy there's no extra space at the end there's nothing extra on the end so if output already has the full value of Timothy then why are we coming to this code again we're gonna add another character what character is there to add well here look first name dot substring 7 comma 1 so what's at the 7th position remember this is a base 0 so T is 0 I is 1 M is 2 o is 3 T is 4 H is 5 y is 6 where 7 don't have a 7 we can't go 7 therefore the character at position 7 would throw an error in fact if we zoom out here and actually into this code we get this error argument out of range exception and that exception simply tells us hey you've got you're out of bounds you can't look for position seven in your strings you only have through position six and so there's our error so we now know what our error is because we debug this now we have figured out well why are we going to seven shouldn't we be stopping before that since there's only zero through six so we know we have to look back up here at this four lie on line 18 because we're going one step too far so if I had continued right now it will blow up if I hit stop or shift f5 they'll stop the application from running all right ahead and did that so now our application stopped so you can make modifications now let's look at why this error happened well if we look at this line which is the line we need to inspect we start at zero well the substring is zero base that's great we started zero only say well I plus plus so only one character at a time which is great but I is less than or equal to first name dot length well what's the length of our name it's seven characters because for length that's the number of characters not a array position so does it start at zero it starts at one so if you had a one character first name the length would be one in my case Timothy is seven characters therefore it first named out length is seven characters so I is less than or equal to seven so if I is seven we're gonna run this code that's a problem and that's where we really should have followed the default pattern for a four which is I is less than first name dot length that accounts for the zero based verse is versus the count of the letters if he runs again now the output shows in the screen is Timothy and there is no error so we know we've corrected that problem so that's how to use the debug to identify and solve problems we click on the gutter over here on the left and place a breakpoint we place that breakpoint wherever you want where there's code so if we say you know what I want to start off right off the bat with this spot right here to debug that's fine you can also place more than one at a time so you could place one down here let's place it there and another one over here that's fine that way what you can do is you can when you hit start you can just say okay I'm looking at that they look at that and back and forth and when you're done you can even tickets out half way through now if you hit start again or continue it'll go until its next breakpoint and I can look at output and say okay that's what the output is so it's a lot you can do with breakpoints just on their own but one of the things that is often overlooked is the fact that breakpoints do more than just pause the application in fact breakpoints don't even have to stop the application at all let's look at an example if I hit run notice how the capital T and a capital Eyre together I didn't really want that let's shift this so that is capital T lowercase I uppercase M and so on now identify well why is it doing not the way I expected because here I said I divided by 2 therefore the second letter should be the case that you gave me the third letter should be uppercase so I'm thinking the first caret the first letter and the third letter because these are the ones that are odd now again my logic is flawed because we're a zero base counting system and this is where when you're doing a for loop debugging is great because game that zero-based versus the length total or count those things can get very easily it gets you tripped up which is which and debugging allows you even if you don't have an error to make sure your code is working in the way intent so let's just start with I wanna know every time it's an even letter what it is and what the value of I is so you play a breakpoint right over here on line 22 but instead of stopping application I just wanna know what those days are now I could run this and if I did I could look at I every single time and said okay I is zero now what's the output kind of beginning I could step into and I actually put my breakpoint down here it's a little cleaner because now the output is T but mousing over two different things is kind of pain I could solve that notice I mouse over let's zoom in here when I mouse over there's a little pin right here I can actually pin this and I also go over here and pin the output and now let's zoom back out I is 0 and output is T if I hit f5 to continue to the next time this breakpoint is hit watch what happens to the values of I and output is now 2 and output is Tim I is now 4 and output TI m OT and then i is now 6 and we had a full Timothy okay so that allows us to see what's happening just by pinning these values and if I want me to get rid of them I ask mouse over them and hit the X now the cool thing is I'll leave this one for now if I hit stop notice those pins kind of go away there's a little pin indicator here he mouse over that and I can see it but if I runs again that's pinned still and that's kinda nice but let's go ahead and I also stopped my application so is there a better way to show us what's going on and maybe even what each character is it's being added one at a time we can do is right click on our breakpoint now I notice there's a lot of options here we can delete the breakpoint which I've been doing just by clicking again we can disable the breakpoint and what that essentially does is leaves it as if it wasn't even there but yeah it's still on the sidelines the gutter for when you need it later can re-enable it but we can also add conditions and actions let's look at actions now on this action right here it says log a message to the output window and then we also has option down here continue execution so let's say notice it even gives you kind of example text you can use sodaw sine function would give you the function name so funking name builder that way if you have multiple these breakpoints you would know which one was being called in this case we don't really care but you know what isn't hurt to show you how it works so dollar sign and notice the intellisense that's amazing dollar sign function so that's the function name but colon the value of I is curly brace I closed curly brace and the letter is curly brace first name nose intellisense dot substring i comma one close clear wrist and what that allows us to do is actually see what the value of I is and what the value of that individual letter is by calling this exact same code now I take close notice it says continue execution that means we're not going to pause the application we're is gonna output this message to the output window and keep going get close and now note that our former circle where it looked like this is now this diamond and that indicates we're doing an action on this row so let's go ahead and pin our output window let's go ahead and clear as well and now I'm gonna hit start now a lot of things use the output window point--we there we go so there's our output window and if we look console you i dot person dot funky name builder that's our method name the value of I is zero the letter is T the value of I is 2 the letter is M the value of I is for the letter is T and a value of I is 6 and the letter is y so there's a four times that that breakpoint is hit and the values for both I and T or anything else you wanna show also note the application fully ran meaning it didn't stop anywhere if we wanted to we could change this breakpoint again right click and say actions it changes to pause the application if you wanted to or not let's go ahead and say no don't continue no it's good like the red circle or yes continue now it's that diamond again but next let's look at other options we right click and say conditions now this condition was going to allow us to do is say only hit this breakpoint under certain circumstances for example this breakpoint gets hit when I is 0 to 4 and 6 what if we said that I pass T of lower case I has to be greater than 2 so now it be the only hit this on four and six and if we go ahead and scroll down here let's make sure that's actually set so that set I is greater than two we hit close notice that if we look at the icon here it's a diamond with a white plus in the middle that white plus indicates that we're not always going to hit this breakpoint we have some type of condition on it so let's clear our output window and run this again and notice down here that we have two entries funky named builder the value of I is four letters T and the value is six and the letter is y so only hit those that break point twice once for the T and once for the Y because in the other occasions I was not greater than two therefore we didn't hit that break point so the question you might ask yourself is when would I ever use either the the action or the condition and as a couple of times when I found them valuable one is sometimes you need to debug only at certain points for example you're loading a thousand records and you know that there's an error somewhere around the 600 mark so you don't want to hit that break point every single time so what you do is you hit that break point at say 590 and step through the last ten be for 600 or maybe you put 600 and see what happens and you keep changing that until you know that your break point right before where the areas and then you can look at all the variable values at that error time the other time you might find it valuable is when you only want to break when certain things are true for example you're processing a whole list and you know that whenever you pull up this one users file and process it you have an error but again you don't want to loop through every single user you want to only target that one particular person so you put a condition on it says only if the user ID is this users user ID will you stop at the record there's a lot of advanced cases where either the condition or the action can be really useful so let's go ahead and delete that breakpoint now be so far I have covered these step into or f/11 but let's look at the other options we have so let's put a breakpoint I'm back in program CS and the static void main put a breakpoint right here on line 18 let's hit start and it will stop at that particular line let's clear the console and get that out of the way so here I am paused on line 18 again that's now in yellow meaning the application is running and we're stopped at this point and I say you know what I don't really care to go into this function let's see what happens if I hit step into f11 daddy takes me to the funky name builder method and I go you know what I didn't want to be here you have a couple of options one you could stop the application and restart it and put your breakpoint different spot and that's just dumb especially if your application is big and you have a lot of setup stuff I do it's just messy don't do that instead what you can do is you can do these step out of or step out which is shift f11 or I just click this button here because I use it so rarely but the idea of step out of is we're in this method right here let's go through the entire method hit the return and then when that return sends us back to our caller we're go ahead and pause the application again so it won't pause and hear anymore it will wait until it's all done this and has sent the values back to our caller at that point we'll the application again so let's do that step out so we're in our paused here on line 15 let's do step out note we're back in static void main and if I hit the step into now it goes the next line and the reason it didn't go the next line when I hit step into the first time is because right here this has to be resolved before we can do the console dot write line and so it resolved it by taking us to this method but once it's resolved the actual value had come back now how does this line actually runs it so I hit step into again it goes to the next line which in this case is line 20 so that's one way of doing it but there has to be an easier way in fact there is so if we run this again I am here on this line I say you know what I don't want to go into this method and I know that if I go into the method I hit step out that's not really that efficient what's a better way of doing that and that would be these step over and that's F 10 or this button right up here so what F 10 does is it says don't leave this particular method just step over any calls that would take you into another method so let's see that in action so if I hit the step over or F 10 it's skipped o or the funky name builder went right to line 20 as if we never were there in fact let's do us again in real time I'll move my breakpoint up to here I hit start I'm gonna hit the step over which seems to work just like these step into except for the fact that it doesn't go into any methods that you are calling so I could hit step over right now even though it's not really stepping over anything and it still works so let's hit step over again and again and it kind of skipped over the idea of diving into funky name builder method so that's why the basics of debugging using these breakpoints the idea is if you know where to look or have a good guess of where to look to figure out why your application isn't working you go ahead and click in the gutter on the line number of your offending line in this case we'll do on line 16 and then whenever you run the application if it's a generic breakpoint it's going to stop once we hit this line that's highlighted you could have more than one and you can also right-click on your break point and add conditions or actions to limit what happens to the breakpoint when it's hit or to do something once it is hit and maybe even skip over it so that's a little bit of advanced stuff but the basics are a breakpoint just click and you're good to go if you don't know where to put your breakpoint you're just not sure at all here's a quick tip just put it the beginning and then step through line by line by line and if your application is pretty large yeah a little bit of a problem you're going to need to have a little better idea than that but what this allows you to do is it allows you to look at everything and have no assumptions you know exactly what's happening line by line the other key here is a guide the idea is that these breakpoints aren't just for debugging they're also really helpful in your learning so you know exactly what's going on because you're watching what the compiler is going to do in your application when it's run in real time so you can see how it changes variables and it morphs things and you say you know what I didn't expect that I don't want that and then you know exactly what need to do to fix whatever is happening because you know where the issue is if you have thousand lines of code it's hard to debug but if you have a thousand lines of code and you know that things go horribly wrong on line 893 it's a lot easier to fix and that's what debugging helps you do using breakpoints alright that's it for now thanks for watching it if you like this video please hit like down below I'd also love it if you'd subscribe I got people putting out more content like this in the near future also if you want to have a heads up on the big things are coming out get some behind scenes access and also some bonuses that I send out once in a while go ahead and sign up for my mailing list there's a link down below where you can click on that and just give me your email address in your name and you'll be sign up the list don't worry I won't spam you I'm very careful about that I like to protect your email address as if it were my own alright thanks for watching
Info
Channel: IAmTimCorey
Views: 33,708
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, debugging, breakpoint, debug, breakpoints, c#, visual studio
Id: _YoKJwSzZOg
Channel Id: undefined
Length: 29min 51sec (1791 seconds)
Published: Thu Nov 10 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.