C#: Preprocessor Directives Cheat Sheet

# Preprocessor Directives in C#
#defineIt defines a sequence of characters, called symbol.
#undefIt allows you to undefine a symbol.
#ifIt allows testing a symbol or symbols to see if they evaluate to true.
#elseIt allows creating a compound conditional directive, along with #if.
#elifIt allows for creating a compound conditional directive.
#endifSpecifies the end of a conditional directive.
#lineIt lets you modify the compiler’s line number and (optionally) the file name output for errors and warnings.
#errorIt allows generating an error from a specific location in your code.
#warningIt allows generating a level one warning from a specific location in your code.
#regionIt lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.
#endregionIt marks the end of a #region block.
# The #define Preprocessor
#define symbol
# Conditional Directives
#if symbol [operator symbol]...

Where, symbol is the name of the symbol you want to test. You can also use true and false or prepend the symbol with the negation operator.

The operator symbol is the operator used for evaluating the symbol. Operators could be either of the following:

  • == (equality)
  • != (inequality)
  • && (and)
  • || (or)

Leave a Reply

Your email address will not be published. Required fields are marked *