Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Preprocessor instructions for C#


May 11, 2021 C#


Table of contents


The preprocessor instructions for the C#

Preprocessor instructions instruct the compiler to preprocess the information before the actual compilation begins.

All preprocessor instructions start with . A nd on one line, only blank characters can appear before the preprocessor instructions. P reprocessor instructions are not statements, so they do not have a sign (; End.

The C# compiler does not have a separate preprocessor, but instructions are processed as if they had a separate preprocessor. I n C#, preprocessor instructions are used to function in conditional compilation. U nlike C and C+ instructions, they are not used to create macros. A preprocessor instruction must be the only instruction on that line.

A list of preprocessor instructions for C#

The following table lists the preprocessor instructions available in C#:

预处理器指令 描述
#define 它用于定义一系列成为符号的字符。
#undef 它用于取消定义符号。
#if 它用于测试符号是否为真。
#else 它用于创建复合条件指令,与 #if 一起使用。
#elif 它用于创建复合条件指令。
#endif 指定一个条件指令的结束。
#line 它可以让您修改编译器的行数以及(可选地)输出错误和警告的文件名。
#error 它允许从代码的指定位置生成一个错误。
#warning 它允许从代码的指定位置生成一级警告。
#region 它可以让您在使用 Visual Studio Code Editor 的大纲特性时,指定一个可展开或折叠的代码块。
#endregion 它标识着 #region 块的结束。

#define preprocessor

#define symbol constants with preprocessor instructions.

#define allows you to define a symbol so that the expression returns true by using the symbol as an expression passed to the #if directive. Its syntax is as follows:

#define symbol

The following procedure illustrates this point:

#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

Conditional instructions

You #if to create a conditional directive using the directive. C onditional instructions are used to test whether a symbol is true. If true, the compiler executes code #if between the next instruction and the next instruction.

The syntax of conditional instructions:

#if symbol [operator symbol]...

Where symbol is the symbol name to be tested. You can also use true and false, or place a negative operator in front of the symbol.

Operator symbols are operators that evaluate symbols. A can operator can be one of the following operators:

  • == (equality)
  • != (inequality)
  • && (and)
  • || (or)

You can also use parentheses to group symbols and operators. C onditional instructions are used to compile code when debugging a version or compiling a specified configuration. A conditional instruction that #if a command must be displayed to terminate with a #endif order.

The following program demonstrates the use of conditional instructions:

#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