So how do I specify debug only in .NET/C#
-
In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?
-
In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?
-
In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?
Like this:
#if DEBUG
Console.WriteLine("Debug version");
#endifFrom what I can see in MSDN, the calls to Debug methods are not included in the release build (while calls to Trace methods are): "If you use methods in the
Debug
class to print debugging information and check your logic with assertions, you can make your code more robust without impacting the performance and code size of your shipping product. In Visual Studio 2005 projects, creating a debug build enablesDebug
. For information on how to disableDebug
, see the Visual Studio 2005 documentation. In contrast, in Visual Studio 2005 projects, Trace is enabled by default for both release and debug builds, so code is generated for all trace methods in both release and debug builds. Therefore, you can use Trace to instrument release builds." --- b { font-weight: normal; } -
Cool, thanks man.
-
In the good old days I'd do something like this #ifdef DEBUG // Bunch of debug only code ... #endif To be more specific, I have some debug spew that uses the System.Threading assembly. How would I exclude this in the debug case? using System.Threading; Also, I am using System.Diagnostics.Debug.WriteLine. Is it safe to assume that these calls be removed from my final assembly?
You should check out the ConditionalAttribute[^], which allows you to cleanly separate conditionally compiled code from the regular code. Josh
-
You should check out the ConditionalAttribute[^], which allows you to cleanly separate conditionally compiled code from the regular code. Josh
Hmm never heard of this before. Have you used it? What concerns me is two things: 1. You can't use an attribute in the middle of a code block for stuff like writing to console only during debug and 2. VS will automatically hide a section of code depending on what's defined for the current configuration. Does this work with the attribute as well?
-
Hmm never heard of this before. Have you used it? What concerns me is two things: 1. You can't use an attribute in the middle of a code block for stuff like writing to console only during debug and 2. VS will automatically hide a section of code depending on what's defined for the current configuration. Does this work with the attribute as well?
Dustin Metzgar wrote:
1. You can't use an attribute in the middle of a code block for stuff like writing to console only during debug
That's the whole point. The attribute allows you to remove debug-only (or whatever-only) code from your "real" code. Since I started using it, I almost never use preprocessor directives anymore because it is just so much cleaner to factor the test/debug code out.
Dustin Metzgar wrote:
2. VS will automatically hide a section of code depending on what's defined for the current configuration. Does this work with the attribute as well?
No, but if you put all of your Conditional methods in one #region, or in a partial class extension, you'll have the same effect. Josh