Search code examples
c#exereverse-engineering

Can #if DEBUG in C# become true in the released binary?


I have something like this in my code that checks for user's license:

// C# code:

#if DEBUG
    MakeLicenseValidForever();
#else
    CheckLicense();
#endif

Now, I need to know if these directives get saved in my released binary or not. If they do get saved, then a user can make #if DEBUG return true and bypass the checks. I need to know if #if DEBUG is safe.

PS1: Obviously I use release mode for distribution.

PS2: I asked my question here first, but since it's not getting any attention, I ask it here again!


Solution

  • No, preprocessor directives are processed at compile time, namely, the compiler won't compile the section into the resulting binary. It's more like code comments, which are discarded during compilation, you can even put invalid statement there and the compiler won't complain about it.

    In general, however, your code would be compiled to intermediate language(IL), which isn't native machine code and be decompiled pretty easily. If you want protection better use some AOT compilation technology or obfuscator.