C# DirectivesUse preprocessor directives to influence program compilation. Test define, if and region.
Directives. Compilers operate in phases. In an early phase the program is read. Statements called preprocessor directives are handled.
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
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.
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.
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.
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.Unreachable
It always helps to read books about a subject. In the C# specification, I learned about the syntax of directives. These are "unsophisticated" macros.
Quote: A preprocessing directive always occupies a separate line of source code and always begins with a "#" character and a preprocessing directive name (The C# Programming Language).
Quote: The preprocessor is a relatively unsophisticated macro processor that works primarily on lexical tokens (The C++ Programming Language).
Many useful directives exist in the C# language. Directives, along with comments, have no effect on program runtime. They instead change how a program text is compiled.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.