Preprocessor Directive in C#

In this article we will see when and how to use some of the preprocessor directives in C# programming.

Let's take a small break from Design Patterns and learn a small, interesting and somewhat unnoticed concept.

What is a Preprocessor Directive?

Preprocessor directives are commands that are interpreted by the compiler and affect the output or behavior of the build process.

We will see some of them now.

  • #region name: Marks the beginning of a region of code; has no compilation effect
  • #endregion name: Marks the end of a region of code; has no compilation effect.
  • #define identifier: Defines a compilation symbol
  • #if expression: If the expression is true, compiles the following section.
  • #elif expression: If the expression is true, compiles the following section however the #elif is not evaluated if a previous #if is true
  • #else: If the previous #if or #elif expression is false, compiles the following section.
  • #endif: Marks the end of an #if construct
  • #warning message: Displays a compile-time warning message
  • #error message: Displays a compile-time error message

#region and #endregion

Sometimes we come across a situation where we want to give a particular group of code a name.

#region MathsLogic

int number1 = 0;

          int number2 = 0;

          Console.WriteLine("Enter 2 numbers");

          string strNumber1 = Console.ReadLine();

          string strNumber2 = Console.ReadLine();

          int.TryParse(strNumber1, out number1);

          int.TryParse(strNumber2, out number2);

          int Sum = number1 + number2;

          Console.WriteLine("Your Sum is " + Sum);

#endregion

Use of regions makes code readable, intuitive and easy to manage (Visual Studio, for example, allows you to easily hide or display regions).

#define

  • A compilation symbol is an identifier that has only two possible states. It is either defined or undefined.
  • The #define directives can be used only at the top of a source file, before any C# code is listed and used to define a compilation symbol.
  • The scope of a symbol created using a #define is the file in which it was defined.

#define Test1
#define Test2
 
using System;
using System.Collections.Generic;
using
System.Linq;

#if, #elif, #else, #endif

Sometimes we do write some code which is just needed during development mode, or required only in some conditions.

In the above statement not needed means even not required to compile.

We can do this with the help of these four directives.

#if Test1
          Console.WriteLine("Test1 Defined");
#elif Test2
          Console.WriteLine("Test1 not Defined but Test2 Defined");
#else
          Console.WriteLine("Both Test1 and Test2 are undefined");
#endif

Here Test1 and Test2 are compilation symbols created using #define.

  • Output is:

    Test1 Defined
     
  • Now place "#define Test1" at the top.

    Now the output is:

    Test1 not Defined but Test2 Defined
     
  • Now place both "#define Test2" and "#define Test1" at the top.

    Now the output is:

    Both Test1 and Test2 are undefined

The main difference between working this way and using traditional if…else with global variables is that here the code won't even be compiled if it finds the symbol is not defined.

Download the attached samples for a practical demonstration.

Thanks for reading,

Hope you enjoyed and stay tuned for more such articles. Comments are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all