#if Not Debug in c#?

C#vb.net

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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)

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

I think something like will work

 #if (DEBUG)
//Something
#else
//Something
#endif

Solution 5 - C#

     bool isDebugMode = false;
#if DEBUG
     isDebugMode = true;
#endif
    if (isDebugMode == false)
    {
       enter code here
    }
    else
    {
       enter code here
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser278618View Question on Stackoverflow
Solution 1 - C#CodeNakedView Answer on Stackoverflow
Solution 2 - C#Aaron DanielsView Answer on Stackoverflow
Solution 3 - C#VaccanoView Answer on Stackoverflow
Solution 4 - C#KhanZeeshanView Answer on Stackoverflow
Solution 5 - C#Luiz Flavio Cavalcanti da SilvView Answer on Stackoverflow