Preprocessor directives
suggest changeSyntax
#define // Defines a compiler symbol.
#undef // Undefines a compiler symbol.
#warning // Generates a compiler warning. Useful with #if.
#error // Generates a compiler error. Useful with #if.
#line // Overrides the compiler line number (and optionally source file name). Used with .
#pragma warning [disable|restore] // Disables/restores compiler warnings.
#pragma checksum “” “” “” // Validates a source file’s contents.
#region // Defines a collapsible code region.
#endregion // Ends a code region block.
#if // Executes the code below if the condition is true.
#else // Used after an #if.
#elif // Used after an #if.
#endif // Ends a conditional block started with #if.
Remarks
Preprocessor directives are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress compilation of part of the file by removing sections of text. Preprocessor lines are recognized and carried out before macro expansion. Therefore, if a macro expands into something that looks like a preprocessor command, that command is not recognized by the preprocessor.
Preprocessor statements use the same character set as source file statements, with the exception that escape sequences are not supported. The character set used in preprocessor statements is the same as the execution character set. The preprocessor also recognizes negative character values.
Conditional Expressions
Conditional expressions (#if
, #elif
, etc) do support a limited subset of boolean operators. They are:
==
and!=
. These can only be used for testing whether the symbol is true (defined) or false (not defined)&&
,||
,\!
()
For example:
#if !DEBUG && (SOME_SYMBOL || SOME_OTHER_SYMBOL) && RELEASE == true
Console.WriteLine("OK!");
#endif
would compile code that prints “OK!” to the console if DEBUG
is not defined, either SOME_SYMBOL
or SOME_OTHER_SYMBOL
is defined, and RELEASE
is defined.
Note: These substitutions are done at compile time and are therefore not available for inspection at run time. Code eliminated through use of #if
is not part of the compiler’s output.
See Also: C# Preprocessor Directives at MSDN.