Debugging Code - C# Mastery Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
as you learn more about c-sharp and how it works it's really important to visually see the code running and executing as well as being able to confirm or test our assertions of what we feel is right in code in this lesson we will learn how to use a very powerful tool inside of Visual Studio called the debugger using good debugger is called debugging you will often hear developers talk about debugging code the term is used in two ways first the literal way meaning you are actually using a debugging tool to help you debug the code secondly the general term used by developers saying they are debugging code simply meaning they are investigating a problem in code or testing code out developers tend to use the term debugging whether they are using a debugger or not for example checking a website for books running your application through a set of conditions that a user has reported a problem with and investigating the problem in general finding bugs does not always involve attaching the debugger so the term debugging code is generally used in both ways the debugger in Visual Studio is very powerful if not the most powerful debugger I know and learning to use it efficiently will set you apart from other developers let's write some simple code that has a intricate issue and use the debugger to help assist us finding and fixing the problem let me write a quick bit of code what do you think the output of this code will be will it crash the console due to dividing by zero will the answer be zero or will it be something else let's run the code and see the issue we're faced with and as you can see here the output is 8 I bet you didn't have that as one of the expected results so we need to debug this code to find out what's wrong without a debugger this simple issue would be very hard to find for most people the debugger allows us to see what each value is line by line and then narrow down where the issue lies when you press f5 you are running this debug menu item start debugging Visual Studio compiles the code runs the application and then attaches another program called the debugger into the running applications memory it then inspects the running code controls the execution flow of the application and can inspect and manipulate the memory for now it's good enough to know that pressing f5 allows us to inspect pause run and even edit our application live and see the results it is incredibly powerful let's use the debugger to solve our problem start by clicking the gray bar to the left of the line numbers this adds a red dot and highlights the line of code in red in fact the first line of code that executes in our application is not this line it's as the opening braces above let's click on the gray bar here to add what is called a breakpoints which is what this red dr. is to the line above and we will click the red dot below to remove the breakpoint from the line below now press f5 to run the application notice how this time focus has brought back to visual studio and the line we have the breakpoint on is highlighted yellow this means this is the next line of code to be executed by our application once we step over this line while it is yellow the application is not running it is paused and all its memory is viewable and we can investigate what we like in the application while it is paused see the yellow arrow in the red dot over to the left this shows the line of code in the application that is about to be run this line of code has not yet been executed this is an important thing to note the line that is yellow is about to be run but has not yet been run remember in the beginning when I mentioned the application is run line by line from the first line of code entering the block of main to the end and sequentially while now you can see this happening this is your proof of knowledge and learning it is incredibly important to prove your assertions when learning code if I tell you that code executes line by line top to bottom inside the main function of your application then you want to prove it to yourself don't just take it as gospel as indeed there are instances where this is not true now we are debugging our application you can officially say that you are debugging code so how do we progress and let the program run to the next line of code we can press f10 or we can go to debug and step over you can see now the breakpoint above us turn back to red which simply indicates it as a breakpoint that when hit will stop our code but the next line in yellow is console dot write line if we take a look at the console window we can see nothing has been output because this line has yet to be run pressing f10 to step over the line we can now go back to the console and see that that line of code has now been execute in the words hello world have been printed remember when pressing f10 to make your visual studio is in focus to capture the key press to inspect variables hover your mouse over them notice how a is currently zero that's because as mentioned in previous lessons each type has its own default value before we assign it a value in the case of all numbers the default value is 0 if we want to inspect a valuable as we are stepping over code without having to hover over them every time we can right-click on the variable and click Add watch you can see down here now in the watch window we have the variable name a and the value 0 this window will stay active and up to date as we step through code meaning we won't have to hover over a to check its value every time we press f10 now let's press f10 again to execute this line of code and step to the next or alternatively you can hover over any line of code and click this button the little green play button so everywhere to press the Run button here it would execute this line of code as well as this line of code and brake on this line of code so you can think of it as a run application too meaning if we started at the top and we hovered over this bottom line and clicked play it would run all of these code lines and break here it's up to you your preference when stepping through code I wouldn't recommend using the menu to step over because it's long-winded I typically press f10 to jump to the next line and step line by line or if you know you want to run to a certain point you can hover over and click this button for now I'm going to press f10 to run one more line of code now that this line assigning the value want to a has been run you can notice the watch window has updated the variable a with a new value 1 and has also highlighted the value in red this indicates since the last step in code the value has changed so since the last time this window was evaluated the number has changed press f10 again and you can see this number has gone back to white indicating that there was no change since the last time we stepped over code as press f10 once more to execute this result and find out what the answer is we could hover over the result variable to see the value or we could right-click and add to watch however you can also use the locals window down here this is like the watch window but it automatically includes all the variables contained in the current code block in this case inside of our main application plus any variables available in the scope if we go to the locals window we can see we have the arcs coming from the mains parameters we have the amv variables and we also have the result which you can see now is infinity so our code did not crash dividing by zero this is because floating-point numbers unlike integers are capable of representing both positive and negative infinity so far so good our code is behaving as expected 1 divided by 0 is indeed infinity this issue then must lie with the console.writeline call what happens inside the ratline code can be found on github as dotnet core is open source or we can see the source code by using tools like resharper I rarely install extra tools in Visual Studio as it slows down the idea as well as introduce bugs into your executing code and your environment I have seen this happen many times and it's still the same today so I do not recommend adding additional tools to Visual Studio unless absolutely necessary we can look up the source code on github by going to google and type in dotnet core github followed by the code that you are trying to find such as console.writeline this is typically the quickest way to find it otherwise you can go to github.com forward slash net and then search for the code inside there instead of going down that rabbit hole first I will share with you some knowledge I have that you don't yet to help solve problem when we pass in console dot write line a variable the console is expecting a string texts are right to the screen not a number however if we stop this code for a moment and check out the possible options to pass in we can pass in a string as expected or we can pass boolean x' characters character arrays decimals doubles floats in Long's many other types we haven't yet covered this in c-sharp but almost every variable object and type can call dot to string you will learn this in future lessons but also that is happening inside of right line when a non string value is passed in is it's calling dot to string on the object passed in so this console dot write line is equivalent to doing result dot to string let's run the code now and see if we get the same result and you can see we do it still prints the number eight to help narrow down this issue let's move this code the converting of the double to a string into another variable of string and then pass that into console dot write line the reason for this is so that we can inspect this variable now and see what the result is before it goes into write line to see if the issue is the results dr. string call let's remove the breakpoint from the top add the breakpoint to just below the execution of result string and press f5 to run the code if we now inspect the locals window we can see the result string is actually showing this infinity symbol that means we are telling the console to write the string infinity but when we do this it outputs the number eight as we can see in the locals window the result is an infinity symbol so the conversion and therefore the variable passed into right line is all correct and as we expect that means the book is somehow in console dot write line and how it chooses to write the string to the console or it is in the console window itself trying to display the string at this point without the knowledge I already have we may have to dig into source code of right line to investigate further however I happen to know that the default character encoding something we'll discuss in future lessons of the console is called code page 850 and this has no symbol for infinity code page 850 is basically an ms-dos character map for all characters that could be displayed in the original ms-dos this is still present even in Windows 10 today but then a symbol for infinity was not part of this character map as I mentioned before don't take my word for it let's check it out press f5 to run the code go back to the console window click the top left then properties and we can see here current code page is 850 a quick google of code page 850 shows us a bunch of additional symbols here that can be represented none of which are an infinity symbol as well as it mentions on top of these additional symbols code page 850 has all the code page of 437 which means it can also display all of these symbols which is your alpha numerics your standard symbols your multiplies x divide some math symbols and some bars however the problem is code page 850 cannot represent the infinity symbol we can also check the output encoding of the console from code to do this I will use a special keyword called 4 for now we will learn about this in a each a lesson and we will simply read the value of the console dot output encoding if we press f5 to run the application and we can either hover over encoding to see the result or we can look in the locals window you can see encoding says system dot text OS encoding clicking this arrow here expand the properties of the OS encoding and we can see code page 850 so what is happening in code is when we tell console.writeline to write a string that contains characters that are not represented by the output encoding of console it has some logic to convert some specific symbols in this case the infinity symbol to the number 8 this is in the source code of right line deep inside the source code the important thing is we have discovered where the issue is and we would know where to look further we have nailed down the issue to the console dot write line call specifically by checking the result string before it goes into the method and knowing that everything up until that point is correct this is the process of elimination and this is a good way to solve problems to fix this problem then we can actually change the output encoding if we hover over output encoding you can see it mentions get or set encoding for the user so to attempt to fix this we will write console the output encoding equals and if we go to system text dot encoding you can see we have some options all these encoding method you will learn about in future lessons for now we will use utf-8 this is the most common Unicode format for text this is the format used in JavaScript in web files and HTML and most of the web you can look up utf-8 on Google if you would like to read more but for now this is outside the scope of this lesson if we put a breakpoint here and a breakpoint here I'm going to show you a few more things with the debugger press f5 to run the code and you can see we hit the breakpoint as expected you can press f10 to step over to the next line but what if we now want to just continue execution of our program until we hit the next breakpoint meaning we wanted to stop at this first line we wanted to stop at this line here we don't necessarily care now about stopping between these two points yet pressing f10 as you have seen steps line by line and does not let your program continue to execute so in order to continue to execute you can go to debug and continue or as you can see you can just press f5 again so if we press f5 our code will now run until it hits the next breakpoint in this case output and code in here so let's press f5 and you can see it's now executed all of these lines of code and then stopped here if we hover over consult the output encoding it has currently text OS encoding which if we expand is currently code page 850 we press f10 to step over that line hover over output encoding again and we can see that it is now successfully changed to encoding utf-8 which is code page 65,000 and one we can see the result string is the correct infinity symbol and if we press f5 again to run the program to completion we can check the console window and now you can see it's correctly output the infinity symbol this is because the infinity symbol is part of the utf-8 character set to fix this problem required a lot of knowledge that I have in a lot of areas well this lesson isn't about you understanding and fix the specific issue it says to see and learn how to use the debugger to help assist you in finding where the issue is in this lesson you have seen how to run the debugger with f5 how to set breakpoints by clicking in the gray bar how to step over code with f10 and how to inspect variables using the locals window or right-clicking on a variable and clicking add watch and then looking at the watch window there are many more things that you can do with the debugger and this is just the tip of the iceberg you will learn them all in future lessons the debugger is very powerful in not only debugging problems but helping you to learn understand and verify your assertions of code
Info
Channel: AngelSix
Views: 9,396
Rating: undefined out of 5
Keywords: c#, software, development, wpf, vue, vue js, animation, web design, wevb, .net core, asp.net core, free, open source, git, tutorial, beginners, real world, javascript, html, css
Id: iWpHF-5uYdg
Channel Id: undefined
Length: 20min 42sec (1242 seconds)
Published: Sun Mar 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.