C# 预处理器指令

顾名思义,预处理器指令是一组在实际编译开始之前进行处理的语句。C# 预处理器指令是影响编译过程的编译器命令。

这些命令指定要编译的代码部分或如何处理特定的错误和警告。

C# 预处理器指令以 # (井号) 符号开头,所有预处理器指令都占用一行。预处理器指令由 换行符 而不是 分号 终止。

C# 中可用的预处理器指令是

C# 中的预处理器指令
预处理器指令 描述 语法
#if 检查预处理器表达式是否为真
#if preprocessor-expression
	code to compile
#endif
#elif #if 一起使用,用于检查多个预处理器表达式
#if preprocessor-expression-1
	code to compile
#elif preprocessor-expression-2
	code to compile
#endif
#else #if 一起使用,用于创建复合条件指令。
#if preprocessor-expression
	code to compile
#elif
	code to compile
#endif
#endif #if 一起使用,用于指示条件指令的结束
#if preprocessor-expression
	code to compile
#endif
#define 用于定义符号
#define SYMBOL
#undef 用于取消定义符号
#undef SYMBOL
#warning 允许我们从代码生成 1 级警告
#warning warning-message
#error 允许我们从代码生成错误
#error error-message
#line 允许我们修改编译器显示的错误和警告的行号和文件名
#line line-number file-name
#region 允许我们创建一个区域,该区域在使用 Visual Studio Code 编辑器时可以展开或折叠
#region region-description
	codes
#endregion
#endregion 指示区域的结束
#region region-description
	codes
#endregion
#pragma 为编译所在文件中的文件提供编译器特殊指令。
#pragma pragma-name pragma-arguments

#define 指令

  • #define 指令允许我们定义一个符号。
  • 当与 #if 指令一起使用时定义的符号将评估为 true。
  • 这些符号可用于指定编译条件。
  • 语法
    #define SYMBOL
  • 例如
    #define TESTING
    这里,TESTING 是一个符号。

#undef 指令

  • #undef 指令允许我们取消定义一个符号。
  • 当与 #if 指令一起使用时,未定义的符号将评估为 false。
  • 语法
    #undef SYMBOL
  • 例如
    #undef TESTING
    这里,TESTING 是一个符号。

#if 指令

  • #if 指令用于测试预处理器表达式。
  • 预处理器表达式可以仅包含一个符号,或者包含符号与诸如 &&(AND)、||(OR)、!(NOT)之类的运算符的组合。
  • #if 指令后面跟着一个 #endif 指令。
  • #if 指令中的代码仅在 #if 测试的表达式评估为 true 时才会被编译。
  • 语法
    #if preprocessor-expression
    	code to compile<
    #endif
  • 例如
    #if TESTING
    	Console.WriteLine("Currently Testing");
    #endif

示例 1:如何使用 #if 指令?

#define CSHARP

using System;
 
namespace Directive
{
	class ConditionalDirective
	{
		public static void Main(string[] args)
		{
			#if (CSHARP)
				Console.WriteLine("CSHARP is defined");
			#endif
		}
	}
}

当我们运行程序时,输出将是:

CSHARP is defined

在上面的程序中,CSHARP 符号在程序开头使用 #define 指令定义。在 Main() 方法中,#if 指令用于测试 CSHARP 是否为 true。仅当定义了 CSHARP 时,#if 指令内的代码块才会被编译。


#elif 指令

  • #elif 指令与 #if 指令一起使用,允许我们创建复合条件指令。
  • 在测试多个预处理器表达式时使用它。
  • #elif 语句中的代码仅在由该 #elif 测试的表达式评估为 true 时才会被编译。
  • 语法
    #if preprocessor-expression-1
    	code to compile
    #elif preprocessor-expression-2
    	code-to-compile
    #endif
  • 例如
    #if TESTING
    	Console.WriteLine("Currently Testing");
    #elif TRAINING
    	Console.WriteLine("Currently Training");
    #endif

#else 指令

  • #else 指令与 #if 指令一起使用。
  • 如果前面的 #if#elif(如果存在)指令中没有表达式为 true,则将编译 #else 指令中的代码。
  • 语法
    #if preprocessor-expression-1
    	code to compile
    #elif preprocessor-expression-2
    	code-to-compile
    #else
    	code-to-compile
    #endif
  • 例如
    #if TESTING
    	Console.WriteLine("Currently Testing");
    #elif TRAINING
    	Console.WriteLine("Currently Training");
    #else
    	Console.WriteLine("Neither Testing nor Training");
    #endif

#endif 指令

  • #endif 指令与 #if 指令一起使用,用于指示 #if 指令的结束。
  • 语法
    #if preprocessor-expression-1
    	code to compile
    #endif
  • 例如
    #if TESTING
    	Console.WriteLine("Currently Testing");
    #endif

示例 2:如何使用条件指令(if、elif、else、endif)?

#define CSHARP
#undef PYTHON
 
using System;
 
namespace Directive
{
	class ConditionalDirective
	{
		static void Main(string[] args)
		{
			#if (CSHARP && PYTHON)
				Console.WriteLine("CSHARP and PYTHON are defined");
			#elif (CSHARP && !PYTHON)
				Console.WriteLine("CSHARP is defined, PYTHON is undefined");
			#elif (!CSHARP && PYTHON)
				Console.WriteLine("PYTHON is defined, CSHARP is undefined");
			#else
				Console.WriteLine("CSHARP and PYTHON are undefined");
			#endif
		}
	}
}

当我们运行程序时,输出将是:

CSHARP is defined, PYTHON is undefined

在此示例中,我们可以看到 #elif#else 指令的用法。在需要测试多个条件时使用这些指令。此外,还可以使用逻辑运算符组合符号来形成预处理器表达式。


#warning 指令

  • #warning 指令允许我们从代码生成用户定义的 1 级警告。
  • 语法
    #warning warning-message
  • 例如
    #warning This is a warning message

示例 3:如何使用 #warning 指令?

using System;
 
namespace Directives
{
	class WarningDirective
	{
		public static void Main(string[] args)
		{
			#if (!CSHARP)
				#warning CSHARP is undefined
			#endif
			Console.WriteLine("#warning directive example");
		}
	}
}

当我们运行程序时,输出将是:

Program.cs(10,26): warning CS1030: #warning: 'CSHARP is undefined' [/home/myuser/csharp/directives-project/directives-project.csproj]
#warning directive example

运行上面的程序后,我们将看到上述输出。文本表示警告消息。这里,我们使用 #warning 指令生成用户定义的警告消息。

请注意,#warning 指令之后的语句也**会执行**。这意味着 #warning 指令不会终止程序,只会**抛出警告**。


#error 指令

  • #error 指令允许我们从代码生成用户定义的错误。
  • 语法
    #error error-message
  • 例如
    #error This is an error message

示例 4:如何使用 #error 指令?

using System;
 
namespace Directive
{
	class Error
	{
		public static void Main(string[] args)
		{
			#if (!CSHARP)
				#error CSHARP is undefined
			#endif
			Console.WriteLine("#error directive example");
		}
	}
}

当我们运行程序时,输出将是:

Program.cs(10,24): error CS1029: #error: 'CSHARP is undefined' [/home/myuser/csharp/directives-project/directives-project.csproj]
The build failed. Please fix the build errors and run again.

我们将看到一些错误,可能如上所示。这里我们正在生成用户定义的错误。

这里需要注意的另一件事是程序将终止,并且行 #error directive example 不会被打印,这与 #warning 指令不同。


#line 指令

  • #line 指令允许我们修改错误和警告的行号和文件名。
  • 语法
    #line line-number file-name
  • 例如
    #line 50 "fakeprogram.cs"

示例 5:如何使用 #line 指令?

using System;
 
namespace Directive
{
	class Error
	{
		public static void Main(string[] args)
		{
			#line 200 "AnotherProgram.cs"
			#warning Actual Warning generated by Program.cs on line 10
		}
	}
}

当我们运行程序时,输出将是:

AnotherProgram.cs(200,22): warning CS1030: #warning: 'Actual Warning generated by Program.cs on line 10' [/home/myuser/csh
arp/directive-project/directive-project.csproj]

我们将上面的示例保存为 Program.cs。警告实际上是 Program.csline 10 生成的。使用 #line 指令,我们已将生成错误的行号更改为 200,文件名更改为 AnotherProgram.cs


#region 和 #endregion 指令

  • #region 指令允许我们创建一个区域,该区域在使用 Visual Studio Code 编辑器时可以展开或折叠。
  • 此指令主要用于组织代码。
  • #region 块不能与 #if 块重叠。但是,#region 块可以包含在 #if 块内,而 #if 块可以与 #region 块重叠。
  • #endregion 指令表示 #region 块的结束。
  • 语法
    #region region-description
    	codes
    #endregion

示例 6:如何使用 #region 指令?

using System;
 
namespace Directive
{
	class Region
	{
		public static void Main(string[] args)
		{
			#region Hello
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			Console.WriteLine("Hello");
			#endregion
		}
	}
}

当我们运行程序时,输出将是:

Hello
Hello
Hello
Hello
Hello

#pragma 指令

  • #pragma 指令用于为编译所在文件中的文件提供编译器特殊指令。
  • 指令可能包括禁用或启用某些警告。
  • C# 支持两个 #pragma 指令
    • #pragma warning:用于禁用或启用警告
    • #pragma checksum:它为源文件生成校验和,将用于调试。
  • 语法
    #pragma pragma-name pragma-arguments
  • 例如
    #pragma warning disable

    示例 7:如何使用 #pragma 指令?

    using System;
     
    namespace Directive
    {
    	class Error
    	{
    		public static void Main(string[] args)
    		{
    			#pragma warning disable
    			#warning This is a warning 1
    			#pragma warning restore
    			#warning This is a warning 2
    		}
    	}
    }
    

    当我们运行程序时,输出将是:

    Program.cs(12,22): warning CS1030: #warning: 'This is a warning 2' [/home/myuser/csharp/directive-project/directive-project.csproj]

    我们可以看到只有**第二个警告**显示在输出屏幕上。

    这是因为,我们最初在第一个警告之前禁用了所有警告,并在第二个警告之前恢复了它们。这就是第一个警告被隐藏的原因。

    我们也可以禁用特定警告而不是所有警告。

    要了解有关 #pragma 的更多信息,请访问 #pragma (C# 参考)

    你觉得这篇文章有帮助吗?

    我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

    以前所未有的方式学习和提高您的编程技能。

    试用 Programiz PRO
    • 交互式课程
    • 证书
    • AI 帮助
    • 2000+ 挑战