164

I have the line in vb code:

#if Not Debug

which I must convert, and I don't see it in c#?

Is there something equivalent to it, or is there some workaround?

H H
  • 263,252
  • 30
  • 330
  • 514
user278618
  • 19,306
  • 42
  • 126
  • 196

5 Answers5

326

You would need to use:

#if !DEBUG
    // Your code here
#endif

Or, if your symbol is actually Debug

#if !Debug
    // Your code here
#endif

From the documentation, you can effectively treat DEBUG as a boolean. So you can do complex tests like:

#if !DEBUG || (DEBUG && SOMETHING)
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
18

Just so you are familiar with what is going on here, #if is a pre-processing expression, and DEBUG is a conditional compilation symbol. Here's an MSDN article for a more in-depth explanation.

By default, when in Debug configuration, Visual Studio will check the Define DEBUG constant option under the project's Build properties. This goes for both C# and VB.NET. If you want to get crazy you can define new build configurations and define your own Conditional compilation symbols. The typical example when you see this though is:

#if DEBUG
    //Write to the console
#else
    //write to a file
#endif
Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
16

Just in case it helps someone else out, here is my answer.

This would not work right:

#if !DEBUG
     // My stuff here
#endif

But this did work:

#if (DEBUG == false)
     // My stuff here
#endif
Vaccano
  • 78,325
  • 149
  • 468
  • 850
5

I think something like will work

 #if (DEBUG)
//Something
#else
//Something
#endif
KhanZeeshan
  • 1,410
  • 5
  • 23
  • 36
-4
     bool isDebugMode = false;
#if DEBUG
     isDebugMode = true;
#endif
    if (isDebugMode == false)
    {
       enter code here
    }
    else
    {
       enter code here
    }
  • 6
    This is a bad answer since it uses run-time logic to handle what could be done at compile time. – antiduh Mar 18 '16 at 18:06
  • Design time T4 templating and the like will need this kind of thing from time to time. – StingyJack Jan 24 '17 at 01:05
  • the advantage of using run time over compile time is that it is infinitely easier to debug, since you don't have to recompile. It also reduces the number of versions of code you could compile, which again is easier to support. if anything, using run time logic over compile time is an advantage, though in this case I would still prefer the more concise handling – J Scott Feb 16 '22 at 16:45
  • Absolutely not what was asked. – jeancallisti Jun 30 '23 at 10:43