I have this program here:
namespace TodoPlus {
using System.Diagnostics;
public class LameProg {
public LameProg() {}
public static void Main(string[] args) {
int a = 2;
int b = 3;
Debug.Assert(a == b, "Bleh");
System.Console.WriteLine("Haha it didn't work");
}
}
}
And somehow, Debug.Assert is not working.
I am using Mono 2.10.5 and this is what I use to compile and execute:
$ dmcs LameProg.cs
$ mono ./LameProg.exe
How can I make this work? I wish it to have the same affect as the assert macro in C, which is to say it should just downright crash the program. Is it possible to do this with Debug.Assert or is there some other function that achieves this?
Thanks.
I believe you need two things: the DEBUG attribute to the compiler, and a 'trace listener' for the runtime. I got it to work with
That still doesn't exit immediately on assertion failure as I would have expected, but at least it prints something.
There's another trick: you can add "exit now" behavior through a TraceListener, because Debug.Assert failures trigger a call to Fail() in the trace listener.
You still need to -define:DEBUG (and TRACE?). I personally expect Assert() calls (in DEBUG builds) to stop my program, dump debug info and exit. So, this is how I do it:
In my code I install a custom trace listener to dump stack and add a call to Exit(). And viola! You have an industry standard response to Assert.Fail(). You could also, for example, print timestamps here, etc.
Combine with a call to:
And you're good to go.
Debug.Assert is annotated with [ConditionalAttribute("DEBUG")]. This means that all invocations are removed by the compiler unless the DEBUG preprocessor symbol is defined. Try this:
Mono does not show a dialog box like Microsoft's .NET implementation when an assertion is hit. You need to set a TraceListener, e.g.
Debug.Assert invocations are typically used in debug builds and removed from release builds. If you want to make sure that a certain condition holds, and this check should be present in release builds, use an
if
statement andthrow
an exception: