Is there a version of Debug.Assert () that will check the condition at the output?

advertisements

I need a version of Debug.Assert() that, in a release build, will still execute the code in the condition parameter but will not show an assertion dialog if the assertion fails. Is there such a tool in .NET 3.5 or will I have to implement this myself (if I even can)?


Use Trace.Assert for this, it works in release mode as well. See the documentation on how to use listeners to use another method than making a dialog come up. An excerpt:

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by the <clear> Element for <listeners> for <trace>, the <remove> Element for <listeners> for <trace>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

So for example:

#if (!DEBUG)
    System.Diagnostics.Trace.Listeners.Clear();
#endif