C#的#define #if用法

C#的#define、#undef、#if、#endif用法

1、 简单的#if、#endif

cs 复制代码
#if true
MessageBox.Show("Now show msg");
#endif

#if false
MessageBox.Show("Don't show msg");
#endif

2、搭配#define用法

cs 复制代码
//首先#define和#undef都要定义在文件最上方,所有using之上
//但可以在上面写注释
#define flag_show
#define flag_test
using System.IO;
// ...
// using

//namespace{...
//class{...

#if flag_show
MessageBox.Show("Now show msg");
#endif

#if undeclare
MessageBox.Show("Don't show msg");
#endif

//如果在#define flag_show下面有一行 #undef flag_show
#if flag_show
MessageBox.Show("Don't show msg");
#elif (!flag_show & flag_test)
MessageBox.Show("Now show msg");
#endif
Summary:
  • #if 后接的是已定义-true || 未定义-false
  • 可以把#define看成true,没有#define或者#undef看成`false',再重新组合给#if、#elif使用
相关推荐
c#上位机11 小时前
wpf之TextBlock
c#·wpf
almighty2720 小时前
C# WinForm分页控件实现与使用详解
c#·winform·分页控件·c#分页·winform分页
almighty2721 小时前
C#实现导入CSV数据到List<T>的完整教程
c#·csv·格式转换·c#导入数据·csv数据导入
程序猿多布1 天前
Lua和C#比较
c#·lua
csdn_aspnet1 天前
使用 MongoDB.Driver 在 C# .NETCore 中实现 Mongo DB 过滤器
mongodb·c#·.netcore
csdn_aspnet1 天前
使用 C# .NETCore 实现MongoDB
mongodb·c#·.netcore
上位机付工1 天前
上位机通信速度有多快?
开发语言·c#·上位机·plc
FuckPatience2 天前
C# 修改基类List中某一元素的子类类型
c#·list
玉面小君2 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia