泉州科技云网站建设,网站开发服务税率是多少,引擎网站推广法怎么做,网站建设如何跑单子C# 条件编译
C# 条件编译#xff1a;根据不同的需求#xff0c;编译生成不同的程序版本#xff0c;条件编译是一种编译预处理命令#xff0c;它是在编译代码之前对源代码进行处理。它可以根据条件#xff0c;决定是否编译某段代码 条件编译的三种形式#xff1a; 第一种…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()方法。