#480 – Pre-Processing Directives

Pre-processing directives are elements that you can include in your source code that are not part of the source code itself.  Instead, they instruct the C# compiler to perform some particular action, like skipping compilation of a section of the code.

The pre-processing directives available in C# include:

  • #define/#undef – Define/undefine condition compilation symbols (see #if)
  • #if/#elif/#else/#endif – Skip compilation of a section of code based on the existence of a conditional compilation symbol
  • #line – specifies a line number to use when reporting errors
  • #error – issue errors during compilation
  • #warning – issue warnings during compilation
  • #region/#endregion – define a collapsible code region, to improve readability
  • #pragma warning – disable or restore certain compiler warnings
  • #pragma checksum – generate a checksum for an ASP.NET page, for debugging purposes

#172 – Nested if Statements

The statement to be executed when an if statement’s boolean expression resolves to true can be another if statement.

            if (bob.IsSingle)
                if (TheGameIsOnToday())
                    WatchGame();
                else
                    PlayVideoGames();

Because the inner if/else is a single statement, it can follow the boolean expression of the outer if as the single statement to execute if the expression resolves to true.

This might lead to some confusion about which if statement the else applies to.  An else clause belongs to the last if that doesn’t have an else clause.

You could make this logic more clear by enclosing the inner if/else in braces.

            if (bob.IsSingle)
            {
                if (TheGameIsOnToday())
                    WatchGame();
                else
                    PlayVideoGames();
            }
            else
                DoWhateverWifeSays();

#170 – The else Portion of an if Statement

An if statement can optionally include an else keyword and block of statements, which will execute if the boolean expression for the if statement evaluates to false.

            if (age >= 50)
            {
                Console.WriteLine("Hey, you should consider joining AARP!");
                DisplayDetailsOfAARPMembership();
            }
            else
            {
                Console.WriteLine("You're still a young pup");
                DisplayInfoAboutVideoGames();
            }

#169 – The if Statement Must Always Include a Boolean Expression

In C++, an if statement can use a conditional expression that resolves to a numeric value.  The statement following the if statement will execute when this expression resolves to any non-zero value.  This form of the if statement is not allowed in C#.

            uint leaves = CountLeavesInBackyard();

            // Works in C++, but is not allowed in C#
            if (leaves)
            {
                RakeLikeHeck();
            }

Since the if statement must use a boolean expression in C#, we’d rewrite the code as:

            if (leaves > 0)
            {
                RakeLikeHeck();
            }

#168 – Use if Statement to Conditionally Execute a Block of Code

You can use the if statement in C# to check the value of a boolean expression and execute a block of code only if the expression is true.

            if (userAge >= 50)
            {
                Console.WriteLine("Hey, you should consider joining AARP!");
                DisplayDetailsOfAARPMembership();
            }

If the value of the userAge variable is greater than or equal to 50, the block of code will be executed.  If the age is less than 50, the block will not be executed and the program will continue with the first statement after the block of code.

#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

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