Category Archives: debugging

Make a simple event logger in C#

Usually the debugger lets you examine how a program works quite effectively, but sometimes it’s useful to use an event logger to record events for later study. For example, when you’re working with mouse events, stopping execution at a break … Continue reading

Posted in classes, debugging, files | Tagged , , , , , , , , , , | Leave a comment

Use XML comments in C#

C# let’s you use XML comments to give extra information to IntelliSense and that can be used to automatically generate documentation. These comments start with three / symbols and should be placed before the program item to which they apply. … Continue reading

Posted in debugging, syntax, tips | Tagged , , , , , , , , , , | 2 Comments

Use the Conditional attribute to make a method non-callable in C#

The Conditional attribute makes a method callable depending on whether a compile-time constant is defined. If the constant is not defined, then the compiler ignores calls to that method. The following code makes the Test subroutine callable if either the … Continue reading

Posted in attributes, debugging | Tagged , , , , , , , , | Leave a comment

Get the executing method name in C#

This example shows how you can get the method name for the currently executing method. Normally you don’t need to get that name (you can just look at the code you’re typing), but sometimes it’s useful for standard tracing and … Continue reading

Posted in debugging, Uncategorized | Tagged , , , , , , , , , | Leave a comment

Validate a switch statement that uses values from an enumerated type in C#

This example shows how you can validate a switch statement that uses values defined in an enumerated type. Enumerated types and switch statements often go hand-in-hand. You define an enumerated type and then later use a switch statement to see … Continue reading

Posted in debugging | Tagged , , , , , , , , , , , , | Leave a comment

Use Debug.Assert to find bugs in C#

The Debug class provides several methods for use in debugging code. Its Assert method takes a boolean value and throws an exception if the value is false. A second parameter gives the error message that the exception should display. If … Continue reading

Posted in debugging | Tagged , , , , , , , , | 2 Comments

Use try catch blocks to protect against unexpected errors in C#

Most programs cannot anticipate every possible error. You can watch for things like missing files, but it’s hard to stop a determined user from entering “ten” in a TextBox that should contain a number. (Actually you can prevent that by … Continue reading

Posted in debugging, syntax | Tagged , , , , , , , , , , , , , , | Leave a comment

Calculate the factorial function and use Debug.Assert in C#

The factorial of a number N is written N! and has the value 1 * 2 * 3 * … * N. By definition, 0! = 1. The value N! gives the number of permutations of a set of N … Continue reading

Posted in algorithms, combinatorics, debugging, mathematics | Tagged , , , , , , , , , , , | Leave a comment

Use listeners to send Debug and Trace information into a file in C#

By default, the Debug and Trace class send messages to the Console window. Sometimes it might be nice to capture that text and save it in a file. This example shows how you can do that. The Debug and Trace … Continue reading

Posted in debugging | Leave a comment