#507 – You can #define Other Symbols within a #if Block

Within the body of an #if/#endif pair, you can include #define or #undef directives.  If the body of the #if does contain #define or #undef directives, it must appear before the first token of the containing source file.

#define DOGSBARK

#if DOGSBARK || DOGSGROWL
#define DOGSMAKENOISE
#endif

using System;
using DogLibrary;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Dog d = new Dog("Kirby", 12);
#if DOGSMAKENOISE
            d.BarkOrGrowl();
#endif
        }
    }
}

#502 – #define and #undef Scope

When you include a #define or #undef preprocessor directive in a file, the scope in which that conditional symbol is defined (or not defined) is limited to that single file.

For example, suppose that we define the symbol QUIET within a file that creates an instance of a Dog and then calls the Bark method of the Dog object.

// Code from Program.cs
#define QUIET

using System;
using DogLibrary;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Dog d1 = new Dog("Kirby", 12);
            d1.Bark();
        }
    }
}

Let’s also suppose that the Dog.Bark method, which exists in a separate file, compiles differently depending on whether the symbol QUIET is defined.

// Code from Dog.cs
        public void Bark()
        {
#if QUIET
            Console.WriteLine("Arf");
#else
            Console.WriteLine("WOOOOOF!");
#endif
        }

Because QUIET is defined in Program.cs but not in Dog.cs, the Bark method will use the second (not quiet) line.

#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.

 

#499 – Conditional Compilation Symbols Are Defined or Undefined

At any given point in your source code, while the compiler is compiling that section of code, a particular conditional compilation symbol is either defined or undefined.

You define a conditional compilation symbol

  • Using the #define preprocessor directive
  • Using the /define option on the compiler command line
  • Implicitly (for DEBUG and TRACE symbols), when selecting a build option for a project

You undefine a conditional compilation symbol

  • Using the #undef preprocessor directive
  • When reaching the end of a source file

A conditional compilation symbol is either defined or undefined–it has no value.

You check whether a conditional compilation symbol is defined by using the #if or #elif preprocessor directives.

A conditional compilation symbol remains defined in a source file until the end of the containing source file or until encountering an #undef directive (whichever comes first).