C# 预处理器指令
C# 预处理器指令
在本教程中,我们将了解预处理器指令、C# 中可用的指令,以及使用它们的时间、原因和方式。
顾名思义,预处理器指令是在实际编译开始之前处理的语句块。 C#预处理器指令是影响编译过程的编译器命令。
这些命令指定要编译的代码部分或如何处理特定的错误和警告。
C# 预处理器指令以 # (hash)
开头 符号和所有预处理器指令持续一行。预处理器指令由 new line
终止 而不是 semicolon
.
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 代码编辑器时创建可展开或折叠的区域 | #region region-description codes #endregion |
#endregion | 表示一个区域的结束 | #region region-description codes #endregion |
#pragma | 为编译器提供特殊指令以编译它所在的文件。 | #pragma pragma-name pragma-arguments |
#define 指令
#define
指令允许我们定义一个符号。- 与
#if
一起使用时定义的符号 指令将评估为真。 - 这些符号可用于指定编译条件。
- 语法:
#define SYMBOL
- 例如:
#define TESTING
在这里,TESTING 是一个符号。
#undef 指令
#undef
指令允许我们取消定义符号。- 与
#if
一起使用时未定义的符号 指令将评估为 false。 - 语法:
#undef SYMBOL
- 例如:
#undef TESTING
在这里,TESTING 是一个符号。
#if 指令
#if
指令用于测试预处理器表达式。- 预处理器表达式可以只包含一个符号或符号组合以及像
&&
这样的运算符 (AND),||
(或),!
(不)。 #if
指令后跟一个#endif
指令。#if
里面的代码 仅当使用#if
测试的表达式时才会编译指令 计算结果为真。- 语法:
#if preprocessor-expression code to compile< #endif
- 例如:
#if TESTING Console.WriteLine("Currently Testing"); #endif
示例一:如何使用#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
是真是假。 #if
内的代码块 指令仅在 CSHARP
时编译 已定义。
#elif 指令
#elif
指令与 #if 指令一起使用,让我们创建复合条件指令。- 在测试多个预处理器表达式时使用。
#elif
里面的代码 仅当使用该#elif
测试的表达式时才会编译指令 计算结果为真。- 语法:
#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
(如果存在)指令为真,#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
指令允许我们从代码中生成用户定义的一级警告。- 语法:
#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
.警告实际上是在 line 10
生成的 通过 Program.cs
.使用 #line
指令,我们已将行号更改为 200
和 AnotherProgram.cs
的文件名 产生了错误。
#region 和#endregion 指令
#region
指令允许我们创建一个可以在使用 Visual Studio 代码编辑器时展开或折叠的区域。- 该指令仅用于组织代码。
- #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# 参考)。
C语言