preprocessor directive

Time:2022-11-25

C# preprocessor directives

Preprocessor directives instruct the compiler to preprocess information before actual compilation begins.

All preprocessor directives begin with #. And on a line, only whitespace characters may appear before a preprocessor directive. Preprocessor directives are not statements, so they do not end with a semicolon (;).

The C# compiler does not have a separate preprocessor, however, instructions are processed as if there were a separate preprocessor. In C#, preprocessor directives are used to work in conditional compilation. Unlike C and C++, they are not used to create macros. A preprocessor directive must be the only directive on the line.

List of C# preprocessor directives
The following table lists the preprocessor directives available in C#:

1. Preprocessor Directive Description
2. #define It is used to define a series of characters that become symbols.
3. #undef It is used to undefine a symbol.
4. #if It is used to test whether the symbol is true.
5. #else It is used to create compound conditional instructions, used together with #if.
6. #elif It is used to create compound conditional directives.
7. #endif Specifies the end of a conditional instruction.
8. #line This lets you modify the compiler's line count and (optionally) the filename where errors and warnings are output.
9. #error It allows an error to be generated from a specified location in the code.
10. #warning It allows generating a first-level warning from a specified location in the code.
11. #region It allows you to specify an expandable or collapsible code block when using the outline feature of Visual Studio Code Editor.
12. #endregion It marks the end of the #region block.
13. #define preprocessor
14. The #define preprocessor directive creates symbolic constants.

#define allows you to define a symbol such that, by using the symbol as an expression passed to a #if directive, the expression will return true. Its syntax is as follows:

#define symbol
The following program illustrates this:

example

#define PI
using System;
namespace PreprocessorDAppl
{
   class Program
   {
      static void Main(string[] args)
      {
         #if (PI)
            Console.WriteLine("PI is defined");
         #else
            Console.WriteLine("PI is not defined");
         #endif
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following result:

conditional instruction

You can use the #if directive to create a conditional directive. Conditional instructions are used to test whether a symbol is true. If true, the compiler executes the code between #if and the next directive.

Syntax for conditional directives:

#if symbol [operator symbol]...

where symbol is the name of the symbol to test. You can also use true and false, or place a negation operator before the symbol.

Common operators are:

== (equal to)
!= (not equal to)
&& (and)
|| (or)
You can also group symbols and operators using parentheses. Conditional directives are used to compile code in debug builds or when compiling for a specified configuration. A conditional directive that begins with an #if directive must be explicitly terminated with an #endif directive.

The following program demonstrates the usage of conditional directives:

example

#define DEBUG
#define VC_V10
using System;
public class TestClass
{
   public static void Main()
   {

      #if (DEBUG && !VC_V10)
         Console.WriteLine("DEBUG is defined");
      #elif (!DEBUG && VC_V10)
         Console.WriteLine("VC_V10 is defined");
      #elif (DEBUG && VC_V10)
         Console.WriteLine("DEBUG and VC_V10 are defined");
      #else
         Console.WriteLine("DEBUG and VC_V10 are not defined");
      #endif
      Console.ReadKey();
   }
}

When the above code is compiled and executed, it produces the following result:

DEBUG and VC_V10 are defined