1.40. C# preprocessor instruction

发布时间 :2023-10-12 23:00:06 UTC      

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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.