1.40. C# preprocessor instruction

发布时间 : 2025-10-25 13:34:40 UTC      

Page Views: 14 views

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.

1.40.1. C# preprocessor instruction list #

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.

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 

The following procedure illustrates this:

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(); } } } 

When the above code is compiled and executed, it produces the following results:

PI is defined 

1.40.3. Conditional instruction #

You can use the #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.

Syntax for conditional instructions:

#if symbol [operator symbol]... 

Among them 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

Common operators are:

  • == (equal to)

  • != (not equal to)

  • && (with)

  • \|\| (or)

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 #if instruction begins with a conditional instruction that must be displayed with a #endif command terminated.

The following program demonstrates the use of conditional instructions:

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(); } } 

When the above code is compiled and executed, it produces the following results:

DEBUG and VC_V10 are defined 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。