.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.

相关推荐
ModestCoder_12 小时前
ROS Bag与导航数据集技术指南
开发语言·人工智能·自然语言处理·机器人·具身智能
卡提西亚12 小时前
C++笔记-34-map/multimap容器
开发语言·c++·笔记
2***B44912 小时前
C++在金融中的QuantLibXL
开发语言·c++·金融
A***071713 小时前
C++在游戏中的阴影渲染
开发语言·c++·游戏
2401_8370885013 小时前
Redisson的multilock原理
java·开发语言
合作小小程序员小小店13 小时前
桌面开发,在线%超市销售管理%系统,基于vs2022,c#,winform,sql server数据
开发语言·数据库·microsoft·c#
Q***l68714 小时前
C++在计算机图形学中的渲染
开发语言·c++
0和1的舞者14 小时前
《网络编程核心概念与 UDP Socket 组件深度解析》
java·开发语言·网络·计算机网络·udp·socket
惜棠14 小时前
visual code + rust入门指南
开发语言·后端·rust