.Net预处理器指令

1.最常用的预处理器指令#region #endregion,来定义可在大纲中折叠的代码区域.

#region MyClass def

public class MyClass

{

static void Main()

{

}

}

#endregion

2.定义符号预处理器指令:来定义或取消定义条件编译的符号:

#define:定义符号。

#undef:取消定义符号。

3.条件预处理器指令:#if,#elif, #else,endif.

可以在代码文件的顶部通过#define/#undef 指令定义符号,用于指定程序代码编译的条件,可以使用 #if 、#elif 、#endif来测试符号.

当您将#define定义的符号用作传递给 #if 指令的表达式时,此表达式的计算结果为 true,将#undef定义的符号用作传递给 #if 指令的表达式时,此表达式的计算结果为 false.

符号可用于指定编译的条件。 可通过 #if 或 #elif 测试符号。 还可以使用 ConditionalAttribute 来执行条件编译,如下例所示:

#define CalledTestM1

#undef CalledTestM2

#define DEBUG

#undef TRACE

using System;

public class TestDefine

{

static void Main()

{

//调用TestConditionalM1

TestConditionalM1();

//不调用TestConditionalM2

TestConditionalM2();

#if (DEBUG)

Console.WriteLine("Debugging is enabled.");

#endif

#if TRACE

Console.WriteLine("Tracing is enabled.");

#endif

}

Conditional("CalledTestM1")

static void TestConditionalM1() { Console.WriteLine("Conditional M1 is Called"); }

Conditional("CalledTestM2")

static void TestConditionalM2() { Console.WriteLine("Conditional M2 is Called"); }

}

// Output:

// Conditional M1 is Called

// Debugging is enabled.

相关推荐
Wenweno0o1 天前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
chenjingming6661 天前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
cch89181 天前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳1 天前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发1 天前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense1 天前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎1 天前
STL 栈 队列
开发语言·c++
勿忘,瞬间1 天前
数据结构—顺序表
java·开发语言
张張4081 天前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533991 天前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python