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

Time:10-31

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!

CodePudding user response:

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.

CodePudding user response:

C# is a language which is compiled into byte code, eg your statements are getting processed during compilation. Conditional statements, used by compiler, are accessable and compilable only if they are defined. In another words, compiler do not see any other statements and your code is safe

  • Related