#112 – Conditionally Compiling Code in Debug Builds

By default, the C# project wizard sets up every project to have two different build configurations–Debug and Release.

Debug builds (as compared with Release builds):

  • Create executables in \bin\Debug directory
  • Do not optimize code
  • Define the DEBUG symbol
  • Write out full debug info

You build your projects in the Debug configuration during development, for the best debugging experience.  Then, when your program is ready to ship, you build the Release configuration and ship that version.

You may want to include certain code in your program that you use only for debugging purposes–so you include it in only the Debug build.  You do this using the #if directive, checking the DEBUG variable.

 static void Main(string[] args)
 {
#if DEBUG
     DoSomeLogging();   // Only do this in Debug build
#endif
    uint x = 0x1234;
    x &= 0x0020;
 }

Code within the #if/#endif directives will not be included in the release build.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: