Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

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

What is different between using #if (DEBUG) and System.Diagnostics.Debugger.IsAttached in visual studio? Are there cases of the DEBUG flag being set, but no debugger attached, or cases when a debugger could be attached when the DEBUG flag is not set?

question from:https://stackoverflow.com/questions/7073266/if-debug-vs-system-diagnostics-debugger-isattached

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

#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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...