Conditional Compilation

Conditional compilation is the inclusion or exclusion of certain blocks of code depending upon the project settings. For example, if the extension creates a log file every time it is loaded, this behavior would not be desirable in the final version of the extension because it is slow and wastes system resources. Conditional compilation should be used to eliminate unnecessary code when the release version of the extension is built.

By default, Visual Studio 2005 defines the DEBUG preprocessor directive in the debug configuration of a project and excludes the DEBUG directive in the release configuration of the project. This directive is very useful in excluding code used specifically for debugging when the release version of the extension is built.

The DEBUG directive is used by adding code similar to the following:

        System.Diagnostics.Debug.WriteLine("debug output");
    #If Debug Then
        System.Diagnostics.Debug.WriteLine("debug output")
    #End If

The code between the #If and #End If is included only if the Debug directive is present.

Send comments about this topic to Microsoft

Build date: 2/16/2009