PrashantUnity

Preprocessor

May 19th, 2022 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. Preprocessor Syntax
  2. The preprocessor directives are easily distinguished from normal programming code in that they start with a hash sign (#). They must always occupy a line that is separate from anything else, except for single-line comments. Whitespace may optionally be included before and after the hash sign.
  3. #line 1 // set line number
  4. Conditional Compilation Symbols
  5. A conditional compilation symbol is created using the #define directive followed by the symbol’s name. When a symbol is defined, it will then cause a conditional expression using that condition to be evaluated as true. The symbol will remain defined only within the current source file, starting from the line where the symbol is created.
  6. #define Symbol
  7. The #undef (undefine) directive can disable a previously defined symbol.
  8. #undef Symbol
  9. Conditional Compilation
  10. The #if and #endif directives specify a section of code that will be included or excluded based on a given condition. Most often, this condition will be a conditional compilation symbol.
  11. #if Symbol
  12.  // ...
  13. #endif
  14. Just as with the C# if statement, the #if directive can optionally include any number of #elif (else if) directives and one final #else directive. Conditional directives may also be nested within another conditional section. In longer conditionals, it is good practice to add comments to the #endif directives to help keep track of which #if directive they correspond to.
  15. #if Professional
  16.  // ...
  17. #elif Advanced || Enterprise
  18.  // ...
  19. #else
  20.   #if Debug
  21.  // ...
  22.   #endif // Debug
  23. #endif // Professional
  24. Diagnostic Directives
  25. There are two diagnostic directives: #error and #warning. The #error directive is used to abort a compilation by generating a compilation error. This directive can optionally take a parameter that provides an error description.
  26. #if Professional && Enterprise
  27.  #error Build cannot be both Professional and Enterprise
  28. #endif
  29. Similar to error, the #warning directive generates a compilation warning message. This directive will not stop the compilation.
  30. #if !Professional && !Enterprise
  31.  #warning Build should be Professional or Enterprise
  32. #endif
  33. Line Directive
  34. Another directive that affects the compiler’s output is #line. This directive is used to change the line number and optionally the source filename that is displayed when an error or warning occurs during compilation. This is mainly useful when using a program that combines the source files into an intermediate file, which is then compiled.
  35. #line 500 "MyFile"
  36. #error MyError // MyError on line 500
  37. Region Directives
  38. The last two directives are #region and #endregion. They delimit a section of code that can be expanded or collapsed using the outlining feature of Visual Studio.
  39. #region MyRegion
  40. #endregion
  41. Just as the conditional directives, regions can be nested any number of levels deep.
  42. #region MyRegion
  43.  #region MySubRegion
  44.  #endregion
  45. #endregion
Add Comment
Please, Sign In to add comment