Pointers and Arrays in C# - Preprocessor Directives
(Page 4 of 4 )
Preprocessor directives supply the compiler with additional information about regions of code. The most common preprocessor directives are the conditional directives, which provide a way to include or exclude regions of code from compilation. For example:
#define DEBUG
class MyClass
{
int x;
void Foo()
{
# if DEBUG
Console.WriteLine("Testing: x = {0}", x);
# endif
}
...
}
In this class, the statement inFoois compiled as conditionally dependent upon the presence of theDEBUGsymbol. If we remove theDEBUGsymbol, the statement is not compiled. Preprocessor symbols can be defined within a source file (as we have done), and they can be passed to the compiler with the/define:symbol command-line option.
The#errorand#warningsymbols prevent accidental misuse of conditional directives by making the compiler generate a warning or error given an undesirable set of compilation symbols. See Table 4-3 for a list of preprocessor directives and their actions.
Table 4-3. Preprocessor directives and their actions Preprocessor directive | Action |
#define symbol | Defines symbol. |
#undef symbol | Undefines symbol. |
#if symbol [operator symbol2] ... | symbolto test. |
| | operators are ==, !=, &&, and ||followed by #else, #elif, and #endif. |
#else | Executes code to subsequent #endif. |
#elif symbol [operator symbol2] | Combines #elsebranch and #iftest. |
#endif | Ends conditional directives. |
#warning text | text of the warning to appear in compiler output. |
#error text | text of the error to appear in compiler output. |
#line [ number ["file"] | hidden] | numberspecifies the line in source code; fileis the file-name to appear in computer output; hiddenspecifies that the compiler should generate debugger information (this feature was added in Visual C# 2003). |
#region name | Marks the beginning of an outline. |
#end region | Ends an outline region. |
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.
|
|