The preprocessor instruction instructs the compiler to preprocess the information before the actual compilation begins.
All preprocessor instructions start with #. And on a line, only white space characters can appear before preprocessor instructions. Preprocessor instructions are not statements, so they do not end with a semicolon (;).
The C # compiler does not have a separate preprocessor, but instructions areprocessed as if they were a separate preprocessor. In C #, preprocessorinstructions are used to play a role in conditional compilation. Unlike C and C++, they are not used to create macros. A preprocessor instruction mustbe the only instruction on the line. The following table lists the preprocessor instructions available in C#: Preprocessor instruction Description #define It is used to define a series of characters that become symbols. #undef It is used to undefine symbols. #if It is used to test whether the symbol is true. #else It is used to create compound conditional instructions to be used with # if. #elif It is used to create compound conditional instructions. #endif Specifies the end of a conditional instruction. #line It allows you to modify the number of lines of the compiler and (optionally)the file name that outputs errors and warnings. #error It allows an error to be generated from a specified location in the code. #warning It allows first-level warnings to be generated from a specified location inthe code. #region It allows you to specify a block of code that can be expanded or collapsedwhen using the outline feature of Visual Studio Code Editor. #endregion It marks the end of the #region block. The following procedure illustrates this: When the above code is compiled and executed, it produces the following results: You can use the Syntax for conditional instructions: Among them Common operators are: You can also group symbols and operators in parentheses. Conditional directives are used to compile code when debugging a version or when compiling a specified configuration. One with The following program demonstrates the use of conditional instructions: When the above code is compiled and executed, it produces the following results: 1.40.1. C# preprocessor instruction list #
1.40.2. #define preprocessor #
#define
preprocessor instructions create symbolic constants.
#define
allows you to define a symbol so that it can be passed to the
#if
instruction, which will return
true
. Its syntax is as follows:#define symbol
Example #
#define PI
using System;
namespace PreprocessorDAppl
{
class Program
{
static void Main(string[] args)
{
#if (PI)
Console.WriteLine("PI is defined");
#else
Console.WriteLine("PI is not defined");
#endif
Console.ReadKey();
}
}
}
PI is defined
1.40.3. Conditional instruction #
#if
directive to create a conditional instruction. Conditional instructions are used to test whether the symbol is true. If true, the compiler executes
#if
code between and the nextinstruction.#if symbol [operator symbol]...
symbol
is the name of the symbol to test. You can also use the
true
and
false
, or place a negative operator before the symbol
==
(equal to)
!=
(not equal to)
&&
(with)
\|\|
(or)
#if
instruction begins with a conditional instruction that must be displayed with a
#endif
command terminated.Example #
#define DEBUG
#define VC_V10
using System;
public class TestClass
{
public static void Main()
{
#if (DEBUG && !VC_V10)
Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && VC_V10)
Console.WriteLine("VC_V10 is defined");
#elif (DEBUG && VC_V10)
Console.WriteLine("DEBUG and VC_V10 are defined");
#else
Console.WriteLine("DEBUG and VC_V10 are not defined");
#endif
Console.ReadKey();
}
}
DEBUG and VC_V10 are defined