Chr*_*isF 25
#if是一个预处理器命令.
最常见的用法(有些人可能会说是滥用)是让代码只能在调试模式下编译:
#if DEBUG
Console.WriteLine("Here");
#endif
Run Code Online (Sandbox Code Playgroud)
一个非常好用(如StingyJack所指出的)是允许轻松调试Windows服务:
static void Main()
{
#if (!DEBUG)
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
// Debug code: this allows the process to run as a non-service.
// It will kick off the service start point, but never kill it.
// Shut down the debugger to exit
Service1 service = new Service1();
service.<Your Service's Primary Method Here>();
// Put a breakpoint on the following line to always catch
// your service when it has finished its work
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}
Run Code Online (Sandbox Code Playgroud)
这意味着运行释放模式将按预期启动服务,而在调试模式下运行将允许您实际调试代码.