Main Site Documentation



i saw today in the exmaples that they used this in the code .

 #if DEBUG

So that made me think, if one does not use the #if and i switch to release mode, when then happens to the Debug.Print(e.Message); ?

In other words, after i disconnect the usb debug cable and turn the board on, is it still chewing up some cycles trying to do it, or is it smart enough to realize that its not connected and does not bother with it.


In release build all #DEBUG sections will be omitted. In debug build the calls will execute.


but that is if the “#if DEBUG” is in there, what if i remove the “#if DEBUG” and just leave the Debug.Print.


Then the Debug.Print will be called.


Unfortunately, this is true and is what I would consider a bug in NETMF. In the full .NET, the Debug class is skipped during a release build and it is therefore unnecessary to put in a #if block.


well thats good to know, and thanks for the heads up.

I wonder why there are so many examples that do not use the #if DEBUG.


I guess because that what it is in most cases - an example. It is up to you to make your final production code optimized for performance and such.


ya, but how is anyone supposed to learn if the examples are not teaching good practices.


I think this is especially confusing (or maybe misleading is a better word) for people coming from C/C++, where they’re used to things getting #ifdef’ed out when DEBUG isn’t defined…

If I had a vote, I’d vote that the Debug class got compiled into stubs or, better, nothing at all when built in release mode. That maybe or have a new class that gets compiled away into nothing in release builds and calls the existing Debug stuff in debug builds…


It should be consistent with the full .NET and not get compiled at all unless the DEBUG symbol is defined.

I’ve requested this of the NETMF team. Please vote for it.


I often #ifdef out debug.print statements even when doing debug builds (I don’t like to depend on just the distinction between a debug adn release build.) You don’t always want them to be working as it can really slow things down and it can be helpful to be able to selectively turn groups of them on/off independently. You might also find that it might be helpful to have a few left on in a release build so you can connect with MFdeploy and monitor a device during long term testing.

For production code I would add a few lines to squirt out a warning if you leave any of the debug.prints on when doing a release build.


[quote]For production code I would add a few lines to squirt out a warning if you leave any of the debug.prints on when doing a release build.

Interesting idea… How do you detect this? Is there a OnDebugPrint event or something that could be handled to blink an LED or something?


I think he is talking about compile time warning.


Is this subject documented somewhere? I wonder how many people here know about this.


Do you mean C# preprocessor directives?

P.S. Forum bug. Looks like the script doesn’t respect url tags and cuts off .aspx in the above link.


There is no such compile time warning that I’m aware. I gathered that Jeff is already providing such a warning. Is this something you’re already doing or a warning you wish for?


@ Architect, no, is there documentation that basically says when going from .net to .netmf we no longer automatically remove the Debug.Print.

To bad the forum does not have a poll feature, i would bet that many do not know about this.


No there is not, but you can create your own with #warning directive.


I don’t see how that would help notify you that you forgot to #if a debug function. If you you remembered to #warning then you would certainly remember to #if. Is there a global application of #warning I’m not understanding? Sorry, it’s still early…


Or you can make sure that Debug.Print is not called in release by doing something like this:

        static void DebugPrint(string text)
            #if DEBUG

or if you still want to use it in release, but prefer to be warned about it

        static void DebugPrint(string text)
            #if !DEBUG
                #warning  Debug.Print is used in release build


You have to use DebugPrint wrapper after that.