Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

example:

#if DEBUG
    throw;
#else
    throw new exc // I want to use autocomplete here but can't because it's greyed out
#endif

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

like image 797
user1306322 Avatar asked Apr 02 '18 11:04

user1306322


1 Answers

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.

enter image description here

Update


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

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

enter image description here enter image description here

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.

enter image description here enter image description here


Refer microsoft docs for more info on this:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/debug-compiler-option

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-by-category

like image 124
Pranav Singh Avatar answered Sep 21 '22 02:09

Pranav Singh