[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use the Debug class's Assert method to find bugs in C#

example

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 an assertion fails while you are running in the debugger, you have the option of opening the debugger to the Debug.Assert statement that threw the exception.

Typically you use Debug.Assert to validate conditions such as input and output values in your code. For example, the following Average method uses Debug.Assert to validate the method's input parameter.

// Return the average of the numbers. private float Average(float[] values) { Debug.Assert(values != null, "Values array cannot be null"); Debug.Assert(values.Length > 0, "Values array cannot be empty"); Debug.Assert(values.Length < 100, "Values array should not contain more than 100 items"); // If there are no values, return NaN. if (values == null || values.Length < 1) return float.NaN; // Calculate the average. return values.Average(); }

Before it begins its calculations, the method verifies that the values parameter isn't null, contains at least one item, and contains no more than 99 items. The first two conditions are necessary to calculate a meaningful average.

The last condition (no more than 99 items) is a "sanity check" to see if the method call makes sense. While you're testing the application, if you discover the program calls the method with more than 99 items, you can see if it makes sense. If it does, you can increase the upper limit to 199 or whatever value is appropriate. This lets you catch unexpected situations where the calling code might contain a bug.

Debug.Assert only works when you run a debug build. When you run a release build, Debug.Assert statements are ignored. This lets you flush out bugs during testing and then remove these statements from the final build.

To make this work, you need to ensure that the code can still work even if a Debug.Assert statement fails. In this example, the Average method returns a value even if the values array is null or contains no values.

To make a debug or release build, open the Build menu and select Configuration Manager. In the Active Solution Configuration dropdown, select Debug or Release and then rebuild the solution.

Finally, the Debug class is in the System.Diagnostics namespace, which is not included by default. To make using it easier to use, you may want to include the following line in your code.

using System.Diagnostics;

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.