Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

The most common use for Preprocessor Directives is to intelligently group your code using the #region … #endregion tag (sidenote: Ctrl + M, M is a great keyboard shortcut to expand and contract your regions).  Preprocessor Directives can also be used to tell the compiler which code should or should not be compiled based on the configuration environment.  Wrapping code in an #if DEBUG … #endif block will only execute if the environment is in debug mode.

This proved to be a handy way to debug a situation this morning in a multi-threaded scenario where breakpoints and Console.WriteLine were not giving me enough information.  I added a new tab with initial visibility as collapsed, set it to visible in an #if DEBUG … #endif block, and wrote events to it also in a debug block.

For situations where you want code to execute only in a production environment, use #if !DEBUG … #endif.

View MSDN’s page on Preprocessor Directives for more information.

Posted on Monday, June 21, 2010 1:08 PM .NET , Keyboard Shortcuts | Back to top


Comments on this post: Use Preprocessor Directives for better debugging

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net