什么是在C#中#如果使用的?是在

2023-09-03 02:01:13 作者:没有你我更好

我需要知道#如果在C#中使用...谢谢..

I need to know the usage of #if in C#...Thanks..

推荐答案

#如果是 pre-处理器命令。

这是最常见的用法(其中有人可能会说是滥用)是有code,只有编译在调试模式下:

It's most common usage (which some might say is an abuse) is to have code that only compiles in debug mode:

#if DEBUG
    Console.WriteLine("Here");
#endif

一个很好的使用(如StingJack指出的)是允许Windows服务的调试简单:

One very good use (as StingJack points out) is to allow easy debugging of a Windows Service:

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 
}

来源

这意味着运行释放模式将启动该服务符合预期,而在调试模式下运行将让你真正调试code。

This means that running release mode will start the service as expected, while running in debug mode will allow you to actually debug the code.

 
精彩推荐
图片推荐