#1,126 – Rewriting a Loop to Avoid continue Statement

The continue statement allows jumping to end of the body of a loop, skipping the rest of the code within the loop.  Either the next iteration of the loop executes, or the code following the loop statement executes.

            List<Dog> myDogs = new List<Dog>
            {
                new Dog {Name = "Kirby", Age = 5},
                new Dog {Name = "Jack", Age = 17},
                new Dog {Name = "Ruby", Age = 2}
            };

            foreach (Dog d in myDogs)
            {
                if (d.Age > 15)
                    continue;

                Console.WriteLine("{0} doing a trick", d.Name);
                d.DoATrick();
            }

1126-001
In most cases, you can rewrite the body of a loop and avoid using a continue statement by using an if statement.  The end result is generally more readable.

            foreach (Dog d in myDogs)
            {
                if (d.Age <= 15)
                {
                    Console.WriteLine("{0} doing a trick", d.Name);
                    d.DoATrick();
                }
            }

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

#506 – Using Expressions in #if and #elif Directives

You can use more complex expressions in #if and #elif directives, instead of just checking to see whether a single conditional compilation symbol is defined.

In the example below, the #elif directive triggers if the DOGSBARK symbol is defined and then DOGSGROWL symbol is not defined.

#define DOGSBARK
//#define DOGSWAG
//#define DOGSGROWL

using System;
using DogLibrary;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Dog d = new Dog("Kirby", 12);

#if DOGSWAG
            d.WagTail();
#elif DOGSBARK && !DOGSGROWL
            d.Bark();
#else
            d.BarkAndGrowl();
#endif

        }
    }
}

You can build more complex expressions using parentheses and the && (logical AND) and || (logical OR) operators.

#if (DOGSWAG && DOGSDOTRICKS) || (DOGSDOEVERYTHING)
            d.WagAndFetch();
#endif

#505 – Using the #elif Directive

When you check whether a conditional compilation symbol is defined using the #if, #else and #endif directives, you can include additional clauses within the scope of the #if directive by using the #elif directive.  The #elif directive adds an additional expression to check, if any earlier expressions evaluate to false.

In the example below, we check both the DOGSBARK and the DOGSWAG symbols to determine which line to compile.

//#define DOGSBARK
#define DOGSWAG

using System;
using DogLibrary;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Dog d1 = new Dog("Kirby", 12);
#if DOGSBARK
            d1.Bark();
#elif DOGSWAG
            d1.WagTail();
#else
            d1.JustSitThere();
#endif
        }
    }
}


You can include as many #elif clauses as you like.

#504 – Using the #else Directive

You can conditionally compile code using the #if and #endif preprocessor directive.  You can also use an #else clause between the #if and the #endif.

In the code below, we’ve commented out the definition of DOGSBARK, so the code between the #else and #endif will be compiled.

//#define DOGSBARK

using System;
using DogLibrary;

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

Note that the code after the #if directive is greyed out, while the code after the #else is formatted normally.

#503 – Conditionally Compile Code Using #if / #endif Directives

Once you define a conditional compilation symbol using the #define directive, you can use the #if directive to conditionally compile code when the corresponding conditional compilation symbol is defined.

In the code sample below, the Dog.Bark method is called, because the DOGSBARK symbol is defined and so the line containing the call to Bark is compiled.

#define DOGSBARK

using System;
using DogLibrary;

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

If we don’t define the DOGSBARK symbol, then the compiler doesn’t compile the line between the #if and #endif directives.  In fact, since the compiler doesn’t even look at these lines in this case, they don’t even have to contain valid C# syntax.

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