📜  C#中的预处理程序指令

📅  最后修改于: 2021-05-30 01:20:18             🧑  作者: Mango

C#中的预处理程序指令指示编译器在程序的实际编译开始之前处理给定的信息。它以井号标签符号(#)开头,并且由于这些预处理器不是语句,因此末尾不会添加任何分号。 C#编译器没有单独的预处理器,但是伪指令的处理就像存在一个伪指令一样。除预处理程序指令外,其他任何行均不能存在。

C#中使用的预处理器如下:

Preprocessor Description
#define To define a Symbol
#undef Removes any definition of a symbol
#if Checks if the symbol evaluates to true
#endif Ends the conditional directive which began with #if
#else If the symbolic value of #if evaluates to false the #else directive statements are executed
#elif Creates a compound conditional directive which is executed if the symbolic value is true
#error Creates a user defined error
#warning Creates a user defined warning
#line Modifies the compiler’s default line numbering
#region Specifies a block of code that can be expanded or collapsed
#endregion Specifies the end of a region
#pragma Gives the compiler information for compilation of the file
#pragma warning Used for enabling or disabling warnings
#pragma checksum Creates checksums for source files

示例1:使用#define,#if,#else和#endif让我们通过一些示例来理解这个概念。在下面给出的代码中,我们使用#define定义一个称为shape的符号。因此,这意味着“形状”的评估结果为true。在main内部,我们使用#if检查形状是否存在。由于它确实存在,并且编译器事先知道它,因此#else部分将永远不会执行,并且被编译器视为注释。 #endif用于指示if纠正的结束。

// C# Program to show the use of
// preprocessor directives
  
// Defining a symbol shape
#define shape
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
  
namespace Preprocessor {
  
class Program {
  
    static void Main(string[] args)
    {
        // Checking if symbol shape exists or not
        #if (shape)
           Console.WriteLine("Shape Exists");
        #else
           Console.WriteLine("Shape does not Exist");
  
       // Ending the if directive
       #endif
    }
}
}

输出:

Shape Exists

示例2:使用#warning和#define考虑另一个示例。在下面给出的代码中,我们有意删除了shapeshape_符号的定义。由于编译器找不到这些文件,因此它执行#else指令。在这里,我们生成用户定义的警告和错误。

// C# program to show the Removal of
// definition of shape and shape_
  
#undef shape_
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
  
namespace preprocessor2
{
    class Program
    {
        static void Main( string[] args )
        {
            // Checking if shape exists
            #if (shape)
                Console.WriteLine("Shape Exists");
                  
            // Or if shape_ exists
            #elif (shape_)
                Console.WriteLine("Shape_ Exists" );
                  
            #else
  
            // using #warning to display message that
            // none of the symbols were found
            #warning "No Symbols found"
  
            // Generating user defined error
            #error "Check use of preprocessors"
  
            // Ending if
            #endif
  
        }
    }
}

由于代码中存在错误,因此无法编译该代码。警告和错误本质上执行相同的工作,但是错误将停止代码的编译过程。 Visual Studio将为您提供以下结果:

示例3:使用#region和#endregion #region将一组指令定义为代码块,并且该块由编译器一次编译。 #endregion标记块的结尾。下面的程序对此进行了描述。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
  
namespace preprocessor3
{
    class Program
    {
        static void Main( string[] args )
        {
            char ch = 'y';
  
            // Using #region to define a block of code
            #region
            if (ch == 'y' || ch == 'Y')
                Console.WriteLine( "Value of ch is 'y'" );
            else
                Console.WriteLine( "Value of ch is unknown" );
  
            // Ends the region
            #endregion
        }
    }
}
输出:
Value of ch is 'y'

示例4:使用#pragma警告和#pragma校验和在下面的代码中,我们使用#pragma warning disable禁用所有警告。在main内部,我们生成了一个用户定义的警告,以检查它是否已被禁用。 #pragma checksum用于帮助调试文件。

// C# program to Disables all warnings
  
#pragma warning disable
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Preprocessor4 {
      
class GFG {
      
    static void Main(string[] args)
    {
        // Creating a warning
        #warning "This is disabled"
          
        // Checksum is used for debugging 
        // the file in consideration
        #pragma checksum "Program.cs"
    }
}
}

上面代码的错误列表: