Visual Studio ignores the code inside #if DEBUG / RELEASE scope and doesn't check for errors or autocompletes

c++ debug-only code
c# if debug
c# preprocessor directives best practices
.net core if debug
web-config if debug
c# conditional compilation
visual studio compiler directives
c# define

I've been writing an #if DEBUG, #else, #endif fragment of code, and I noticed that Visual Studio doesn't let me use autocomplete to fulfill partially typed member names, and it doesn't check the greyed out inactive code for errors. The only way I've found to make it care again is to switch the build mode from Debug to Release. But that's inconvenient and feels like there's a better way.


    throw new exc // I want to use autocomplete here but can't because it's greyed out

How do I make VS stop ignoring the other code inside the other configuration's scope of #if DEBUG?

It is purpose of conditional compilation, it is working as intended. With conditional compilation application can ignore certain code at compilation. Your application in Visual Studio is running in Debug Mode so compiler is ignoring code inside #elsepart.

Run your application in Release mode then #else code will be available but #if DEBUGwill not be available.


For checking both #if DEBUG & #else you need to run application twice.

1.Once in debug mode in which code with #if DEBUG like :

here application is in debug mode so #if DEBUG condition code is active..

  1. Run application in release mode for checking code in #else condition. Here other part will be able to use autocomplete & debug too.

Refer microsoft docs for more info on this:

#if preprocessor directive, An elevation of privilege vulnerability exists in Visual Studio when it loads software dependencies. Assembly does not match code for function. and debug your JavaScript applications all within the Visual Studio IDE. even when it's out of scope, whereas the former option may result in constant,� The Debug: Run (Start Without Debugging) action is triggered with ⌃F5 (Windows, Linux Ctrl+F5) and uses the currently selected launch configuration. Many of the launch configuration attributes are supported in 'Run' mode. VS Code maintains a debug session while the program is running, and pressing the Stop button terminates the program.

I know this is a late answer, but it still applies to this day.

TL;DR: this is a VS bug, which affects only affects netstandard/netcore projects (or simply put, the new .csproj format).

The workaround is... nasty! Reload the project file (or close and reopen VS).

The good news: this does not affect compilation :)

You can read more here and here.

Visual Studio 2019 version 16.0 Release Notes, Set debug and release configurations in Visual Studio For managed code and C++ code, debug information can be generated in .pdb files, Creating .pdb files can be useful if you later have to debug your release version. For more information, see Why does Visual Studio require debugger symbol� Edit your code and continue debugging (C#, VB, C++) In most languages supported by Visual Studio, you can edit your code in the middle of a debugging session and continue debugging. To use this feature, click into your code with your cursor while paused in the debugger, make edits, and press F5, F10, or F11 to continue debugging.

At the beginning of the file define both constant:

#define DEBUG
#define RELEASE

Next, use two if condition instead of if/else.

            // autocomplete works
            // autocomplete works

Of course, you must delete/comment out these defines before compiling the project.

In my opinion, this awkward and fraught with mistakes.

Set debug and release configurations, #if(DEBUG) import foo = require('debug'); #else import foo Ignore Regions of code in compiler #829 We cannot call cordova plugins from the desktop. that the compiler will assume to be constants (unless defined in scope). path to /src/ conditional/dev when second one use /src/conditional/release. Visual Studio Languages , .NET Framework > Visual C# . #if DEBUG //Code goes here #endif Marked as answer by Harry Zhu Wednesday, June 23, 2010 7:46 AM;

Support conditional compilation � Issue #449 � microsoft/TypeScript , You can run this code within the Unity Editor, so you can compile the code UNITY_5, #define directive for the release version of Unity 5, exposed in every 5. Log("Stand Alone OSX"); #endif #if UNITY_STANDALONE_WIN Debug. In addition to the basic #if compiler directive, you can also use a multiway test in C#: I was going through some legacy code the other day, refactoring it all over the place and I ran into many blocks of code wrapped in “#if DEBUG”. Of course, after a bit of refactoring in a RELEASE configuration these blocks of code were quickly out of date (and by out of date, I mean no longer compiling). A huge PITA.

Platform dependent compilation - Unity, When you create a new project in your IDE, most IDEs will set up two performance, and doesn't contain the extra debugging information. There are multiple ways to switch between debug and release in Visual Studio. In Code:: Blocks, you should see an item called Build Target in the Compiler toolbar� To debug the app, in your project, set breakpoints on some code. In the Visual Studio toolbar, make sure the configuration is set to Debug, and the browser you want appears in IIS Express (<Browser name>) or Local IIS (<Browser name>) in the emulator field.

0.9 — Configuring your compiler: Build configurations, Maybe the breakpoint hits only after the variable has run out of scope. In that case the In the native code, instead to add breakpoints, just add this : assert( false); When breaking against the shores, just click either 'Retry' to debug or ' Ignore' to continue. Happy debugging with Visual Studio 2015 :) Kochise. Multi-threaded code is notoriously tough to debug. Visual Studio lets you control the execution of multiple threads at once and inspect state across many threads to make the big picture pop. See all the thread call stacks in a single graphical view and evaluate expressions across multiple threads to compare values.

  • Ok so I guess I want to do it another way then if I want my code to be checked for errors and allow autocomplete. Your answer explains why it happens, but not how I can do what I explained.
  • You can check code for error but at one time only #if in debug mode & other code in #else in release mode. I am updating answer
  • Just an idea, remove #if & #else for checking code & getting autocomplete after code is tested & autocomplete is used for all code put #if & #else again.
  • yea, that's basically what I did, but by commenting instead of deleting
  • I really expected there to be a checkbox to force code processing within the inactive scope, but I suppose the intention of the entire thing contradicts my wish.