Hello, my name is Gerald. In these lessons, you will learn how to debug your code in Visual Studio. In this lesson, I will show you how to use breakpoints and code-inspection tools within Visual Studio. Let's take a look at our codes so we can get familiar with it before we start. This is a console application with three functions in it. We have our main function. We have a print numbers function, which runs through a loop and prints the loop variable to the console, and we have a generate random string function, which generates a random string and returns it to the caller. So let's add our first breakpoint. To add a breakpoint, there are two ways, first find the line of code you need to put the breakpoint on. You can right-click it. Move down to the breakpoint menu, and click "insert breakpoint." You can also insert a breakpoint using the breakpoint bar on the left-hand side of your code screen. You can click here, and it'll insert a breakpoint. You can tell by the red highlighting and also these red dots on the breakpoint bar, that breakpoints exist. So let's run our applications so we can see the breakpoints in action. So our program has started. It started executing, and we hit our first breakpoint on print numbers, line 14. You can tell the application has paused at a breakpoint by the start button, now says "continue," indicating the application is paused, and we would need to click "continue" to continue past the breakpoint. You can also tell because the line is highlighted in yellow that we are on this breakpoint. Let's click "continue" to hit our next breakpoint. I clicked "continue," and now the program has paused again at our second breakpoint. You can also tell over here, because there's a yellow arrow, indicating we're stopped at this breakpoint. Let's hit "stop" and take a look at some breakpoint settings. We're going to put a breakpoint in our print numbers function inside of the loop. So I will click here on line 30 to insert a breakpoint. Now, to get to the breakpoint settings, there are two ways, you can hover over your breakpoint bar and click on this cog wheel. Or you can right-click your line of code, go to the breakpoint menu, and you can see your breakpoints. We have "delete breakpoint," which completely removes it. We have "disable breakpoint," which will turn the breakpoint off but does not delete it. And you could later, at a different time, enable the breakpoint. We also have conditions, actions, and labels. First let's take a look at labels. Labels are like a name for your breakpoint. So you want to give it a meaningful label. We'll call this one "Print Numbers Loop." (keys clicking) We'll click "add" and then "okay" to add a label to this breakpoint. The labels will show up in the breakpoints window. So to open the breakpoints window, go to the debug menu on the main menu, go to Windows, and then click "breakpoints" to open the breakpoints window. The breakpoints window will list every breakpoint in your application. And you can see here, in the labels column, we see the name of our breakpoint. There are many options in here. You can add all kinds of columns to keep track of your breakpoints and see information about them. So that was labels. Let's take a look at conditions. Again, I will go to the breakpoint settings and click on "conditions." Conditions are the method to conditionally break on your breakpoint. So if you only want to have your program stop executing when a certain condition is true, you would add a conditional expression here to conditionally stop on your breakpoint. There are also other things here, like hit count. If you would only like to stop on your breakpoint after it's been reached a number of times, you would use the hit count. And then there's another filter option here. We'll add a conditional expression as an example, and we will say, only break and stop at this breakpoint when the loop variable I is equal to five. So our breakpoint is here, and it will only stop execution when our condition is true of I equal to five. Let's close and watch this in action. So, our program is executing, and it's hit our breakpoint. And I'll show you the output here, so you can see that we've passed over the breakpoint five times. It's printed zero, it's printed one, two, three, and four, and now I is equal to five, which was our condition, and our breakpoint has stopped code execution. Now I'll click "continue," and you'll take notice that it'll continue execution and will not break because our condition will no longer be true. So I'll click "continue," and our program has finished execution without stopping at our breakpoint. So that's how you add a conditional breakpoint. Let's take a look at actions. So again, I'll go to the breakpoint settings, and I'll click on "actions," clear everything, and check the "actions" box. The actions is useful for printing something to the output window when the breakpoint is hit. So sometimes you might want to print a message or print a variable, and it'll get sent to the output window. So let's print some text and then the value of I. (keys clicking) So I entered some text here that will be sent to the output window. It'll print, print numbers function, I equals, and then these curly brackets indicate it should print the value of the I variable here in our print numbers function. Let's hit "close" and then watch this in action. We'll click "start," and we can see here in our output window it's sending the value of I to the output. See print numbers function, I equals zero, the whole way through nine. That indicates every time that breakpoint was hit, we sent that text and the value of I to the output window. So this is most useful when you want to print the value of a variable at your breakpoint. Let's hit "stop," and we will add another breakpoint. Let's add a breakpoint here. Well, let's keep the same breakpoint. Let's add one here to our inner loop and hit "start." We're going to take a look at code-inspection tools. So one of the most useful code-inspection tool is the hover. So if you hover over a variable, you'll see a little toolbox open up here, and it'll show us the value of the variable. So we can see the variable I, and its value currently is zero. I will hit "continue" to continue execution. I clicked "continue," and now we have hit our breakpoint again, because we're inside of a loop. Take a look at I, and I can see it has incremented to one. We'll take a look at the limit parameter that was passed in, and we can see the limit equals 10. So this hover tool is very useful for inspecting variables. Now, let's take a look at some more complex objects. Hit "stop," and we'll put a breakpoint in our generate random string function. We'll put it down here at the end. So code will execute the whole way through this function, and we'll hit our breakpoint here at the end of this function. Let's click "start," and we have hit our breakpoint. So let's use the hover tool to take a look at the string builder class. So here, I am hovering over the string builder variable, and I can see the value of it here. If you expand this, you can see even more information about the object. We can see it has a capacity member, which is equal to 16. We can see it has a length member that's equal to nine and a max capacity member that's equal to INT dot max value. This inspection window also gives you access to nonpublic members, so you can see the private members of this variable. You expand that node, and here we have access to view the private variables for the string builder object. And again, you can see the static members of the variable. Here, you can see all the static members of that variable. There's also one more nice code-inspection tool here that can help you visualize, for example, an array. So at the top here, we have our alphabet variable, and it's an array of characters. So I've hovered over the alphabet variable. We get our little window again, and I'll click on "view" to visualize the array. And here it popped up in the visualizer, and I can see the array and all the indexes and their value. There's an "export to Excel" button here, if you want to export this to Excel, but this is very helpful when you're working with lists, arrays, any kind of collection, if you want to visualize it so you can see what's inside of it. So those are the code-inspection tools. Now you know how to use breakpoints and code-inspection tools in Visual Studio. Thanks for watching and stay tuned for our next lesson where we will cover code navigation functions.