什么是在C#中#如果使用的? [英] What is the use of #if in C#?

查看:97
本文介绍了什么是在C#中#如果使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道#如果在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.

这篇关于什么是在C#中#如果使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆