An example. This program uses a #define directive. It defines a symbol named PERL. This needs to be at the top of the source code file.
Then: We can use #if directives in the rest of the file to detect whether a #define was specified.
Here: The program shows that #if PERL will evaluate to true. But #if PYTHON will not.
If: We conditional directives, such as "if" and "elif," and we test the values of our definitions.
C# program that uses define, preprocessor directives#definePERL
static void Main()
Warning, error. The warning and error directives make the compiler complain. This is helpful when generating code—you can force an error to occur if the code is somehow incorrect.
Line. The line directive influences warnings and errors. The compiler will report line numbers based on your custom line directive.
Pragma. The default compilation settings in Visual Studio are usually fine. But with pragma, you can adjust them. You can make some warnings disappear.
So: If you know about a warning and it just annoys you, pragma can help. Sometimes this helps with an unused code warning.
However: The pragma directive seems, in my experience, just to add further complexity to an issue that is already complex.
Region. This directive organizes code. We create named "regions" of code, terminated with endregion. We can then collapse these regions in Visual Studio.
Caution: Other organizational constructs are often better. For example, consider using extra classes or methods to break up your code.
Chaos. Directives lead to code maintenance problems. Bit rot can begin if you use "#if" to remove some code blocks. The program may change so that the code no longer works.
And: Even if the code works, it will not have been tested. Things become more difficult, more dangerous for the programmer.
Bit rot: In large programs, this is a serious issue. Code becomes neglected and might stop working. But in its neglect, we don't know this.
Unreachable: With the pragma directive, we can keep unused code compiling with no warnings. This might help reduce bit rot.