C# Preprocessor Directives

C# Preprocessor directives are known as the set which gives instruction to the compiler to preprocess the information before actual compilation starts. In C# all Preprocessor Directives start with #.

These commands specifies which sections of the code to compile or how to handle specific errors and warnings.

The preprocessor directives are used to help in conditional compilation.

C# preprocessor directive begins with a # (hash) symbol and all preprocessor directives last for one line. Preprocessor directives are terminated by new line rather than semicolon.

The workflow in C# that produces an executable application looks like this.

C# Preprocessor Directives

C# Preprocessor Directives is the concept from the world of compiled languages. A compiled language is one that accepts high-level code, such as C# or C, and converts it to machine code using a compiler. The compiler acts as an intermediary between different manufacturers’ architectures, knowing how to transform code blocks to different architectures such as Intel or AMD processor instructions. The final machine code is not generated by the preprocessor. It just preprocesses the code for the compiler, as its name implies. It includes directives that are assessed ahead of time and affect the compilation process. Preprocessors and compilers are usually viewed as independent entities. Preprocessors can range from simple to complicated, depending on the language.

Syntax: C# Preprocessor Directives

Preprocessor directives in C#:

Given below are the C# Preprocessor directives.

Preprocessor directives Description
#undefThis Preprocessor directive allows you to undefine a symbol.
#define This Preprocessor directive defines a sequence of characters, called symbols.
#endif This Preprocessor directive specifies the end of a conditional directive.
#endregion This Preprocessor directive marks the end of a #region block.
#elif This Preprocessor directive allows creating a compound conditional directive.
#error This Preprocessor directive allows generating an
error from a specific location in your code.
#warning This Preprocessor directive allows generating a level
one warning from a specific location in your code.
#if This Preprocessor directive allows testing a symbol or
symbols to see if they evaluate to true.
#line This Preprocessor directive lets you modify the compiler’s
line number and (optionally) the file name output for errors and warnings.
#else This Preprocessor directive allows creating a compound conditional directive, along with #if.
#region This Preprocessor directive lets you specify a block of code that you can expand or
collapse when using the outlining feature of the Visual Studio Code Editor.

Lets look that the #if preprocessor

Syntax

The #if statement tests for the identifier PD, which does exist, and so the code between #if and #else is compiled into your programbut the code between #else and #endif is not compiled. That code does not appear in your assembly at all; it is as if it were left out of your source code.

Had the #if statement failedthat is, if you had tested for an identifier that did not existthe code between #if and #else would not be compiled, but the code between #else and #endif would be compiled.

Any code not surrounded by #if – #endif is not affected by the preprocessor and is compiled into your program.

using System;

namespace PreprocessorDirectives {
 public  class Program {
    public  static void Main(string[] args) {
         #if (PD)
            Console.WriteLine("PD Run");
         #else
            Console.WriteLine("PD is not Run");
         #endif
        
      }
   }
}