#If DEBUG is ignored (VB.net or C#)

Viewed 32145

I have several of these in my code which have been working fine so far:

#If DEBUG Then    
   ... some code here       
#End If     

Now, i am noticing that, lately, the code inside the " #If DEBUG Then ... #End If" gets execute also in "Release Mode".

This is strange and did not happen before. What could have happened so that the #If DEBUG are now being ignored (they are ignored both in debug in the IDE or the final executable) ?

I have applied Clean, Rebuild, etc.: no luck. Thank you for any hints and help.

-Pam

9 Answers

Had a similar problem where "DEBUG" was never true. Tried by doing an uncheck and check of the "Define DEBUG constant" checkbox and rebuilding everytime but that did not work.

My solution was to define "DEBUG" manually in the "Conditional compilation symbols" textbox for the Debug configuration. When rebuilding, Visual Studio 2019 automatically removed the DEBUG symbol from the textbox (because this indeed should not be there) and from then on it worked again. When i switched from Debug to Release the correct lines got greyed out. This seems to be a possible bug in VS 2019 (16.4.5)?

If you are using VB.NETFramework v4.5 then use like

If Debugger.IsAttached Then
    '... some code here
End If
Related