
Debugging Code
A bug is a programming error that makes a program fail to produce the correct result. The
program might crash, display incorrect data, or do something completely unexpected such as
delete the wrong file.
In this lesson you learn how to use the excellent debugging tools provided by Visual Studio’s
IDE to find bugs in C#. You learn about different kinds of bugs and you get to practice debug-
ging techniques on some buggy examples that you can download from the book’s web site.
DEFERRED TECHNIQUES
Unfortunately at this point in the book you don’t know enough about writing code to be able to
understand and fix certain kinds of bugs. For example, a program crashes if it tries to access an
array entry that is outside of the array, but you won’t learn about arrays until Lesson 16.
So why does this lesson cover debugging when you don’t even know all of the techniques you
need to cause and fix certain kinds of bugs? It makes sense for two reasons.
First, the previous lesson was the first part of the book where you were likely to encounter bugs.
Whenever I teach beginning programming, students start seeing bugs as soon as they write code
that performs calculations like those covered in Lesson 11. These kinds of bugs are easy to fix if
you know just a little bit about debugging but can be extremely frustrating if you don’t.
Second, it turns out that you don’t need to know more advance