当前位置: 首页 > news >正文

C# 条件编译

C# 条件编译

C# 条件编译:根据不同的需求,编译生成不同的程序版本,条件编译是一种编译预处理命令,它是在编译代码之前对源代码进行处理。它可以根据条件,决定是否编译某段代码
条件编译的三种形式:
第一种形式:

#if 条件....
#endif

举个例子:

#if DEBUGDebug.Print("软件测试中");Console.WriteLine("软件测试中");
#endifConsole.WriteLine("Excel梦想家软件代码");Console.ReadKey();

如果你在debug模式下:会打印出“软件测试中”和“Excel梦想家软件代码”,但是如果你在release模式下:只会打印出“Excel梦想家软件代码”
第二种形式:

#if 条件......
#else......
#endif......

例程如下:

#define TRIAL
//#undef TRIAL
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace demo_0809
{internal class Program{static void Main(string[] args){//第二个例子,自定义符号
#if TRIALDateTime endDate = new DateTime(2023, 8, 4);int days = (endDate - DateTime.Now).Days;if (days > 0){Console.WriteLine("您还有{0}天的试用期", days);}else{Console.WriteLine("您的试用期已过,请购买正式版权");Console.ReadKey();return;}
#elseConsole.WriteLine("正式版用户,欢迎使用系统!");
#endifConsole.WriteLine("Excel梦想家软件开始工作");Console.ReadKey();}}
}

自定义符号注意点如下:
#define 符号名字,符号名通常使用英文大写,必须定义在所有using命名空间前面;符号可以被整个项目中的所有文件使用;#undef 符号名字,可以取消已经定义的符号。
如上面的代码所示:##undef TRIAL这句代码被注释掉了,代码只会执行“使用版本”的代码和“endif”后面的代码,如果没有注释掉#undef TRIAL这句代码,会打印出:
“正式版用户,欢迎使用系统!
Excel梦想家软件开始工作

第三种形式:

#if 条件1......
#elif 条件2......
#else......
#endif......

例程如下:

#define FREE_VERSION
#define PRO_VERSION
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace demo1_0809
{internal class Program{static void Main(string[] args){
#if (FREE_VERSION)Console.WriteLine("你使用的是免费版");
#elif (PRO_VERSION)Console.WriteLine("你使用的是专业版");
#elseConsole.WriteLine("你使用的版本未知");
#endifConsole.ReadKey();}}
}

上面写了两个define,会打印出“你使用的是免费版”,如果注释掉“#define FREE_VERSION”,会打印出“你使用的是专业版”,如果两个都注释掉,会打印出“你使用的版本未知”。
补充:特性

#define 高级版
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace demo2_0809
{internal class Program{static void Main(string[] args){play();Console.WriteLine("hello world");Console.ReadKey();}[Conditional("高级版")]static void play(){Console.WriteLine("高级玩法");}}
}

上述例子:会调用play()方法,如果不加“#define 高级版”,那么不会调用play()方法。

http://www.lryc.cn/news/116733.html

相关文章:

  • IntelliJ IDEA如何重新弹出git身份验证窗口
  • 【雕爷学编程】Arduino动手做(200)---WS2812B幻彩LED灯带4
  • 【雕爷学编程】Arduino动手做(201)---DFRobot 行空板03
  • Spring中Bean的“一生”(生命周期)
  • 安卓:LitePal操作数据库
  • 【JavaEE初阶】了解JVM
  • 基于vue2.0和elementUi的vue农历日期组件vue-jlunar-datepicker(插件)
  • Python爬虫——selenium_元素定位
  • 短视频内容平台(如TikTok、Instagram Reel、YouTube Shorts)的系统设计
  • 【git】Git 回退到指定版本:
  • kibana+nginx配置密码 ubuntu
  • Git仓关联多个远程仓路径
  • 使用ffmpeg将m4a及wav等文件转换为MP3格式
  • 【CI/CD】Git Flow 分支模型
  • SpringBoot Thymeleaf模板引擎
  • prometheus部署
  • Flink-Window详细讲解-countWindow
  • React 18 state 如同一张快照
  • EasyPoi导出 导入(带校验)简单示例 EasyExcel
  • 八大排序
  • 网络安全【黑客技术】自学
  • 【网络通信】socket编程——TCP套接字
  • ROS2系统学习番外篇2---用VSCode开发ROS2程序
  • 06 - Stream如何提高遍历集合效率?
  • 【Spring】使用注解的方式获取Bean对象(对象装配)
  • [webpack] 基本配置 (一)
  • 模板学堂|SQL数据集动态参数使用场景及功能详解
  • Wlan——射频和天线基础知识
  • 前端实习周记第三周周记
  • Android 13 Launcher界面——移除Launcher的删除和卸载功能