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.
With symbols like #define, directives can add or remove lines of code, without affecting runtime. Many programs can reside in one. Things become complex.
define, undef
If, Elif, Endif
Line Directive
Pragma Directive
region, endregion
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.
Detail We conditional directives, such as "if" and "elif," and we test the values of our definitions.
#define PERL using System; class Program { static void Main() { #if PERL Console.WriteLine(true); #endif #if PYTHON Console.WriteLine(false); #endif } }
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.
Warning 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.
Detail In large programs, this is a serious issue. Code becomes neglected and might stop working. But in its neglect, we don't know this.
Detail With the pragma directive, we can keep unused code compiling with no warnings. This might help reduce bit rot.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.