The Debug Class

The Debug class, exposed by the System.Diagnostics namespace, provides interaction with the Visual Studio debugger and enables understanding if your code is working correctly via instrumentation that evaluates conditions at a certain point of your code. Basically the Debug class exposes only shared methods and can display contents into the Output window so that you can programmatically interact with the debugger without the need to set breakpoints. Table 5.2 provides an overview of Debug methods.

Table 5.2 Debug Class Methods

image

Debug Output

Saying that the Debug class can display contents to the Output window is true only in part. Developers can use other built-in outputs known as trace listeners to redirect the output. Later in this chapter we provide an overview of trace listeners.

Continuing the code example in Listing 5.1, try to add the following lines of code after the declaration and assignment of the result variable within the Main method:

image

Now run the application and type in a valid (nonempty) string. Figure 5.16 shows how the Output window appears when the runtime encounters the Debug methods.

Figure 5.16 Writing debug information to the Output window.

image

The first line shows the Boolean value of the result variable. The WriteLine method can be useful if you need to monitor objects’ values without breaking the application. This method also adds a line terminator so that a new line can begin. The Write method does the same but does not add a line terminator. The WriteLineIf (and WriteIf) writes a message only if the specified condition is evaluated as True. If you enter a valid string, the WriteLineIf method writes a message. Notice that there is an invocation to the Assert method. This method causes the runtime to show a message box containing the specified message that is shown only if the specified expression is evaluated as False. According to this, if you enter a valid string in the sample application, the expression is evaluated as True therefore, no message is shown. If you instead enter an empty string (that is, press Enter), the runtime shows the dialog represented in Figure 5.17.

Figure 5.17 The Assertion dialog.

image

The Fail method, which is not shown in the example, shows a similar dialog but without evaluating any condition. In Table 5.2, methods descriptions mention trace listeners. We now provide an overview of the Trace class and then an overview of the particular objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.192.79