37

Is it possible to hide fields and/or properties from showing up in the debugger watch window? See, we've got a class here with over 50 private fields, most of which are exposed through public properties. This means we're seeing a duplication of a large number of data in the watch window listing.

Is there any means of controlling this?

5 Answers5

67

Try this attribute:

 [DebuggerBrowsable(DebuggerBrowsableState.Never)]

Use it to hide your backing fields by placing the attribute above the field declaration like this:

class Foo
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    int bar;  // this one will be hidden
    int baz;  // but this one will be visible like normal
}

Keep in mind that the DebuggerBrowsableState enumeration has two other members:

Collapsed: Collapses the element in the debugger.
RootHidden: This shows child elements of a collection but hides the root element itself.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    I don't know why this is marked as valid answer. If the object comes from 3rd party where we can't apply [DebuggerBrowsable] then this is no answer. The question is rather if/how can we config VS to just hide them for us – Pawel Cioch Aug 14 '19 at 16:39
  • @Andrew Hare is there any way to do this for local variables? We have some code that passes around a users password. During pair programming sessions we don't want the users password to be staring their partner in their face. – rollsch May 23 '21 at 02:26
6

Check out the DebuggerBrowsableAttribute:

http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerbrowsableattribute.aspx

In fact, this article has some very useful tips for this area:

http://msdn.microsoft.com/en-us/magazine/cc163974.aspx

You might find that using a DebuggerTypeProxy makes more sense. This allows you to provide a "custom view" of the type.

Martin Peck
  • 11,440
  • 2
  • 42
  • 69
1

I know this is old but you would be much better off with using DebuggerTypeProxy http://msdn.microsoft.com/en-us/library/d8eyd8zc.aspx

this way you don't have to modify your class with ugly attributes and the extra benefit is that you can always look at the real type if you do in fact need to have a look at one of those "hidden" fields.

Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
1

The DebuggerBrowsableAttribute is covered in this other SO question. If you're doing C# heavily then it's a good question to read up on.

Community
  • 1
  • 1
Fung
  • 7,530
  • 7
  • 53
  • 68
-2

You could use autos instead of locals or use watches and only watch the variables of interest...

Shea
  • 11,085
  • 2
  • 19
  • 21