#500 – #define and #undef Must Be at Top of File

In C#, all #define and #undef  preprocessor directives must appear at the top of a source code file, i.e. before any valid C# tokens.  The definition (or un-definition) then remains in effect until the end of the file is reached.

#define QUIET

using System;
using DogLibrary;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Dog d1 = new Dog("Kirby", 12);
#if QUIET
            d1.Bark("Arf");
#else
            d1.bark("WOOOF");
#endif
        }
    }
}

In this case, because the conditional compilation symbol is included directly in the code, Intellisense already knows which of the two Bark methods will be included in the code.  It displays the line that will not be compiled in grey.

In this example, the second Bark method is not compiled at all.

 

Advertisements

#490 – Using the DEBUG Conditional Compilation Symbol

By default, Visual Studio creates a Debug build configuration in which the Define DEBUG constant option is checked.

You can check to see if this constant (or “conditional compilation symbol”) is defined in your code, using the #if directive.

        public void Bark()
        {
#if DEBUG
            Console.WriteLine("DEBUG INFO: Entering Dog.Bark() [{0}]", Name);
#endif

            Console.WriteLine("{0} says woof", Name);

#if DEBUG
            Console.WriteLine("DEBUG INFO: Exiting Dog.Bark() [{0}]", Name);
#endif
        }

Code between the #if DEBUG and matching #endif will be compiled only if the DEBUG compilation symbol has been defined.  By default, this will be true for the Debug build configuration, but not the Release build configuration.

When we select the Debug build configuration and re-build, we see the following output:

When we select the Release build configuration, where the DEBUG symbol is no longer defined, we see the following output:

#115 – Using #if, #else, #endif

When using the #if directive to conditionally compile code if a particular symbol is defined, you can also  using #else to conditionally compile code if the symbol is not defined.

Here’s an example:

#if LOGGING
    DoLotsOfLogging();   // To assist in debugging
#else
    DoMinimalLogging();
#endif

#113 – Conditionally Compiling Code Based on Symbols

In addition to conditionally compiling code based on the DEBUG symbol, you can define your own new symbols and then use the #if directive to indicate that a block of code should only be compiled when that symbol is defined.

For example, let’s assume that you want to easily be able to include/exclude code that does some logging, but you don’t want to necessarily do this in only the Debug configuration.

Select the build configuration in which you want the symbol defined:

Right-click on the project and select Properties.  In the project properties window, click on the Build tab.

Now you can add your own symbol in the “Conditional compilation symbols” textbox:

Finally, use the #if directive to identify code that should only be built when the LOGGING symbol is present.

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