#if (DEBUG) VS System.Diagnostics.Debugger.IsAttached

前端 未结 6 858
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 11:52

What is different between using #if (DEBUG) and System.Diagnostics.Debugger.IsAttached in visual studio? Are there cases of the DEBUG

6条回答
  •  情书的邮戳
    2020-12-24 12:02

    #if DEBUG ensures the code is not included in the assembly at all in release builds. Also, code included by #if DEBUG runs all the time in a debug build - not just when running under a debugger.

    Debugger.IsAttached means the code is included for both debug and release builds. And a debugger can be attached to release builds too.

    It's common to use both together. #if DEBUG is usually used for things like logging, or to reduce exception handling in internal test builds. Debugger.IsAttached tends to just be used to decide whether to swallow exceptions or show them to a programmer - more of a programmer aid than anything else.

提交回复
热议问题