亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# 预处理器指令

C# 预处理器指令

在本教程中,我们将了解预处理器指令、C# 中可用的指令,以及使用它们的时间、原因和方式。

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

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

C# 预处理器指令以 # (hash) 开头 符号和所有预处理器指令持续一行。预处理器指令由 new line 终止 而不是 semicolon .

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 代码编辑器时创建可展开或折叠的区域
#region region-description
	codes
#endregion
#endregion 表示一个区域的结束
#region region-description
	codes
#endregion
#pragma 为编译器提供特殊指令以编译它所在的文件。
#pragma pragma-name pragma-arguments

#define 指令


#undef 指令


#if 指令

示例一:如何使用#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 指令


#else 指令


#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 指令


示例 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 指令

示例 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 指令

示例 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 指令,我们已将行号更改为 200AnotherProgram.cs 的文件名 产生了错误。


#region 和#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 指令

示例 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语言

  1. C# 关键字和标识符
  2. C++ 变量、文字和常量
  3. C++ 数据类型
  4. C 关键字和标识符
  5. C 数据类型
  6. Python 关键字和标识符
  7. Python 正则表达式
  8. Python时间模块
  9. 印刷与数控机床
  10. t 是机械加工中的 SFM?
  11. C - 预处理器
  12. C++ 预处理器