Will #if RELEASE work like #if DEBUG does in C#?

前端 未结 11 1766
时光说笑
时光说笑 2020-11-30 17:39

In all the examples I\'ve seen of the #if compiler directive, they use \"DEBUG\". Can I use \"RELEASE\" in the same way to exclude code that I don\'t want to run when compi

11条回答
  •  日久生厌
    2020-11-30 17:51

    Whilst M4N's answer (#if (!DEBUG)) makes most sense, another option could be to use the preprocessor to amend other flag's values; e.g.

    bool isRelease = true;
    #if DEBUG
        isRelease = false;
    #endif
    

    Or better, rather than referring to whether we're in release or debug mode, use flags that define the expected behavior and set them based on the mode:

    bool sendEmails = true;
    #if DEBUG
        sendEmails = false;
    #endif
    

    This is different to using preprocessor flags, in that the flags are still there in production, so you incur the overhead of if (sendEmails) {/* send mails */} each time that code's called, rather than the code existing in release but not existing in debug, but this can be advantageous; e.g. in your tests you may want to call your SendEmails() method but on a mock, whilst running in debug to get additional output.

提交回复
热议问题