Shino Lex
Shino Lex

Reputation: 525

Does my code inside preprocessor directives included in compiled code if the directive is not present in c#?

I have an application which I want to create a demo for it. I prefer to give another exe for my paid version and different one for demo to prevent people with demo version to crack my paid version. For now I have commented the code I need in demo so whenever I have to create a demo I go over these comments and make the necessary changes. Today I realize the #if directive..

Now my question is if I have a code like this

#if DEMO
   public string test()
   {
     return "blabla";
   }
#endif

and I did not define DEMO directive, so my code gets greyed out obviously and I can't reach this method which is what I was expecting so far. Now I have rebuild my project without defining DEMO directive and I deleted my .pdb file from output folder after that I tried to check my executable with dotpeek and I could not see this test method there.

Can I use this approach to separate my demo and paid version executables? Lets say this test method is a demo method so it wont be seen in paid version if someone cracks it. And same goes for opposite, if this test method is a paid version method and I put it inside #if !DEMO then if I define DEMO and rebuild my exe I won't see this test method there right?

As I mention before I already checked the code and could not see my methods with dotpeek but I just want to be 100% sure that these methods wont seen in programs like dotpeek or anywhere else

Upvotes: 0

Views: 222

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500973

Yes, the code is completely omitted if the symbol isn't defined. It can be absolute garbage code - the compiler ignores it completely; it just looks for the end (either #endif or #else).

The question about whether that suits your needs for demo/paid versions is somewhat different, but the answer from the technical perspective of "what gets included in the output" is simple.

Upvotes: 1

Related Questions