Question

Having recently learned of the DebuggerDisplay attribute, I've found it quite useful. However, one thing that surprises me is that it doesn't have a [ConditionalAttribute("DEBUG")] attribute attached to it. Is there some way to force this or is it a bad idea to try? Or does it not matter for some other reason?

Was it helpful?

Solution

The [ConditionalAttribute("DEBUG")] is only used for optimising out method calls.

If you really want to remove these from your builds you can use #ifdef so that the code is only compiled in release mode.

One thing to bear in mind is that you can still debug binaries in release mode, as long as you have the pdb files it shouldn't matter. Release mode just clears up variables sooner and applies some compiler optimisations

OTHER TIPS

As I often have to debug things in Release configuration builds that don't have the DEBUG directive, I would not want these hints to the debugger to be removed.

However, if you have some proprietary or confidential information in the way you display things when debugging that you don't want to make it into your release build, you may want to consider using the ConditionalAttribute or #if/#elif/#endif preprocessor directives to control what is emitted into your release builds.

For example, you could do:

#if DEBUG
[DebuggerDisplay...]
#endif
public class MyAwesomeClass
{
}

This would ensure the attribute is only emitted when the DEBUG directive is given.

I'll share a pattern that I've come to appreciate using partial.

public partial class MyClass{
    //class details here
}

And then elsewhere:

#if DEBUG
[DebuggerDisplay("DebuggerValue")]
public partial class MyClass{
    //anything needed for debugging purporses
}
#endif

This gives the ability to use DebuggerDisplay or other attributes without cluttering-up the base class.

I've been using a handful of files, all wrapped in #if DEBUG to hold these Debug-Partials. It helps keep the core classes cleaner and I don't have to remember to start/end compiler directives for each attribute.

I would think it would be a bad idea, because a lot of times the thing you're attaching the attribute to has some other use besides just showing it in the debugger, IMO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top