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

前端 未结 3 1985
无人及你
无人及你 2021-01-12 23:54

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

相关标签:
3条回答
  • 2021-01-13 00:31

    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.

    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 :

    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:

    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

    0 讨论(0)
  • 2021-01-13 00:35

    At the beginning of the file define both constant:

    #define DEBUG
    #define RELEASE
    

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

    #if DEBUG
                // autocomplete works
    #endif
    #if RELEASE
                // autocomplete works
    #endif
    

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

    In my opinion, this awkward and fraught with mistakes.

    0 讨论(0)
  • 2021-01-13 00:39

    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.

    0 讨论(0)
提交回复
热议问题