There are lots of small tricks in .net which can be very useful if used wisely. In this post I want to discuss the use of DebuggerBrowsableAttribute. Using this attribute you can control how a member of class will be displayed in debugger windows during debugging. Before that consider small example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Organization { public List<Employee> Employees { get; set; } public Organization() { this.Employees = new List<Employee>(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class Employee { private Guid UniqueGuid { get; set; } public string Code { get; set; } public string Name { get; set; } public Employee() { UniqueGuid = Guid.NewGuid(); } } |
Lets create a instance of Organization and add some sample data:
1 2 3 4 5 6 7 | Organization org = new Organization(); org.Employees.Add(new Employee() { Code = "E01", Name = "Matt" }); org.Employees.Add(new Employee() { Code = "E02", Name = "Tim" }); org.Employees.Add(new Employee() { Code = "E03", Name = "David" }); org.Employees.Add(new Employee() { Code = "E04", Name = "Tom" }); |
Now when you run this program in debug mode and try to view the org in debugger window it will look like this:
Lets try to change it using DebuggerBrowsableAttribute. Make following changes by adding DebuggerBrowsableAttribute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public List<Employee> Employees { get; set; } [DebuggerBrowsable(DebuggerBrowsableState.Never)] private Guid UniqueGuid { get; set; } |
Again run this program in debug mode and try to view the org in debugger window. Now it will look like this
You can see how this small but useful attribue can change the look of the debugger window.