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

相关推荐
kaikaile199512 分钟前
MATLAB计算卫星星下点轨迹
开发语言·算法·matlab
武藤一雄12 分钟前
C# 万字拆解线程间通讯?
后端·微软·c#·.net·.netcore·多线程
赵庆明老师31 分钟前
NET 10 集成Session
缓存·.net
二狗哈42 分钟前
Cesium快速入门15:图元Primitive创建图像物体
开发语言·javascript·3d·webgl·cesium·地图可视化
lljss20201 小时前
C# 定时器类实现1s定时器更新UI
开发语言·c#
白杨攻城狮1 小时前
C# 关于 barierr 心得
开发语言·c#
SEO-狼术1 小时前
Detect Trends with Compact In-Cell Visuals
.net
zhglhy1 小时前
Jaccard相似度算法原理及Java实现
java·开发语言·算法
catchadmin1 小时前
PHP 8.5 容器化实战指南
开发语言·php
nono牛1 小时前
完整bash语法教程:从零到专家
开发语言·chrome·bash