Today I again focus on the attributes that will make your debugging less painfull :). This time my scribbles should appeal to the programmers who prefer keeping their code clean (and adhere to the principle: the less ‘spare’ code the better).
So grab a mug of whatever you like and take a look at DebbugerBrowsable and DebuggerDisplay . The first one may be a good alternative to DebuggerTypeProxy I described in previous post and the second one is just ‘a must’ (really!) that makes life easier.
[DebbugerBrowsable(…)]
Sometimes our object has so many properties, the Variables window in Visual Studio becomes messy. Some members may be totally unimportant while debugging so don’t you think, it would be great to just hide them?
If your answer is ‘yes’ I have a good news for you – you don’t have to write additional class like with the [DebuggerTypeProxy]. The simplest way is just applying [DebbugerBrowsable(…)] attribute to any property! After doing this, you should also set its’ named parameter as:
- Never = 0 – the element will not be shown
public class Vehicle { public int ProductionYear { get; set; } public decimal Price { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] public string Model { get; set; } }
- Collapsed = 2 – the element will be shown as collapsed
public class Vehicle { public int ProductionYear { get; set; } public string Model { get; set; } public decimal Price { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)] public List<string> Owners { get; set; } }
- RootHidden = 3 – if the member is a collection/array, the items (and only items) will be displayed
public class Vehicle { public int ProductionYear { get; set; } public string Model { get; set; } public decimal Price { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public List<string> Owners { get; set; } }
[DebuggerDisplay]
This one is my favorite debugging-helper! Like DebuggerTypeProxy, DebuggerDisplay helps us with data we see while debugging. Everybody knows the pain – you want to preview the members of an object and have to click the “expand” arrow to get the desired information… Because all we see is the type name…
But there is one simple solution for this problem – we can add the DebuggerDisplay attribute to our class and define how the debugger will display our object.
[DebuggerDisplay("This is a {Model} from {ProductionYear} year. Value: {Price}")] public class Vehicle { public int ProductionYear { get; set; } public string Model { get; set; } public decimal Price { get; set; } }
And that’s it! It is also worth mentioning, that you can apply DebuggerDisplay to many members:
– Assembly
– Class
– Struct
– Enum
– Property
– Field
– Delegate
Seems like a blessing, isn’t it? Don’t be so optimistic! The example I mentioned above may cause some problems with the debugging performance. Especially when there is a list of items and debugger has to calculate displayed value for each of them.
But if you want to debug the code smoothly, don’t resign on DebuggerDisplay completely. Just reduce displayed properties to just one and everything should work ok.
PS Please keep in mind, that you can always replace DebuggerDisplay with overridden ToString() method.