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, the source file name.
#pragma warning [disable|restore] // Disables/restores compiler warnings.
#pragma checksum "file" "{guid}" "bytes" // 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
C# preprocessor directives are handled by the compiler. They do not define macros, and they cannot insert another source file. A directive chooses which code is compiled, or how warnings, line numbers, and regions are reported.
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents