C# preprocessor
In this chapter you will learn:
- A list of preprocessor commands available in C#
- All C# Preprosessor directives
- Example for Preprocessor directives
Description
Preprosessor directives mark the code block for compiler.
For example,
#define DEBUG/*from w w w . jav a2 s .co m*/
using System;
class MyClass
{
int x;
void aMethod()
{
# if DEBUG
Console.WriteLine("Testing: x = {0}", x);
# endif
}
}
If DEBUG is defined C# will compile the statement.
We can supply the flag with /define:Symbol through commandline.
The code above generates the following result.
C# Preprosessor directives
The following table lists all C# Preprosessor directives
Preprocessor directive | Action |
---|---|
#define symbol | Defines symbol |
#undef symbol | Undefines symbol |
#if symbol [operator symbol2]... | operators are ==, !=, &&, and || followed by #else, #elif, and #endif |
#else | Executes code to subsequent #endif |
#elif symbol [operator symbol2] | Combines #else branch and #if test |
#endif | Ends conditional directives |
#warning text | warning text for compiler output |
#error text | error text for compiler output |
#line [ number ["file"] | hidden] | number specifies the line in source code; file is the filename to appear in computer output; hidden instructs debuggers to skip over code from this point until the next #line directive |
#region name | beginning of an outline |
#end region | Ends an outline |
C# uses the preprocessor directives to control the compilation.
The preprocessor directives begin with # and it must appear on its own line.
The preprocessor directives conditional compilation is #if
,
#else
and, #endif
, and #elif
.
The #if directive tells the compiler to ignore a section of code unless a specified symbol has been defined.
To define a symbol we use the #define
or set it in the compilation.
#define
directives must be at top of file.
Symbol names are uppercase by convention.
#define TESTMODE//from www. jav a 2 s.com
using System;
class Program
{
public static void Main()
{
#if TESTMODE
Console.WriteLine("in test mode!"); // OUTPUT: in test mode!
#endif
}
}
The output:
To define a symbol assembly-wide, specify the /define switch when compiling:
csc Program.cs /define:TESTMODE,ANOTHER
Preprocessor directives | C# statements |
---|---|
#if | if |
#else | else |
#elif or #else #if | else if |
The ||, &&, and ! operators can be used to perform or, and, and not operations.
Logical Operators Supported by the #if..#endif Directive
Operator | Example | Description |
---|---|---|
== | #if winXP == true | Evaluates to true if the symbol winXP is defined. Equivalent to #if winXP. |
!= | #if winXP != true | Evaluates to true if the symbol winXP is not defined. Equivalent to #if !winXP. |
&& | #if winXP && release | Evaluates to true only if the symbols winXP and release are defined. |
|| | #if winXP || release | Evaluates to true if either of the symbols winXP or release are defined. |
() | #if (winXP || win7) && release | Parentheses allow you to group expressions. Evaluates to true if the symbols winXP or win7 are defined and the symbol release is defined. |
Example
The following code uses #define, #undef, #elif and #else to do the conditional compilation.
#define win7//from ww w . j a v a 2 s. c om
#define release
#undef win2000
using System;
using System.Diagnostics;
class MainClass
{
[Conditional("DEBUG")]
public static void DumpState()
{
Console.WriteLine("Dump some state...");
}
public static void Main()
{
string platformName;
#if winXP // Compiling for Windows XP
platformName = "Microsoft Windows XP";
#elif win2000 // Compiling for Windows 2000
platformName = "Microsoft Windows 2000";
#elif win7 // Compiling for Windows 7
platformName = "Microsoft Windows 7";
#else // Unknown platform specified
platformName = "Unknown";
#endif
Console.WriteLine(platformName);
}
}
To build the example and define the symbols winXP, use
the command csc /define:winXP; Example.cs
.
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- What is Preprocessing Directives #define
- How to use Preprocessing Directives #define
- Example for Preprocessing Directives #define
- Use the Conditional attribute with #define