DebuggerDisplayAttribute
One attribute that I find very useful, but for some reason I always forget what it’s called, is the DebuggerDisplayAttribute. This allows us to display a field or combination of fields within the debugger window, for example when we move the mouse over a variable whilst stepping through code. Instead of just seeing the type name we can display something possibly more meaningful.
[DebuggerDisplay("Name: {FullName}, Role: {OrganisationRole}")] public class User { // ... public string FullName { get; set; } public string OrganisationRole { get; set; } }
The above will now display something like
Name: John Smith, Role: Team Lead
in the debugger.
DebuggerStepThroughAttribute
Even with my poor memory, this is one I remember well as I use it quite a lot.
When in debug mode stepping through code often we end up stepping into properties or other code which maybe makes no sense to step through. Either the code is simple, like a getter simply returning a value or make it’s boiler plate code for calling a webservice.
Using the DebuggerStepThroughAttribute (as below) we can tell the debugger to basically step-over the code contained within the property or method.
public string this[string key] { [DebuggerStepThrough] get { return parameters[key]; } }