We all need to debug our projects and for that we need to some visualization to see values of debug data C# has a attribute called ‘DebuggerDisplay’ which helps developers to visualize data in the way developer wants .
As per MSDN, DebuggerDisplay attributes allows developers of the type, who specifies and best understands the runtime behaviour of that type, to also specify what that type will look like when it is display in debugger.
Display attribute constructor has one argument, a string will displayed in the value column for instances of type. This string will contain {} braces and this text between {} will evaluate as expression. So what we are waiting for lets try it in Visual Studio. I have written following code for that.
Here you can see I have created a class called “Employee” with two variable with DebuggerDisplay Attribute and from main method I have created a object of employee class. Now let’s create debug the code via pressing F5 and see how its works.
As you can see its display data as defined in argument. So now you can format your debugger visualizer screen as per your requirement. That’s it. Hope you like it. Stay tuned for more.
As per MSDN, DebuggerDisplay attributes allows developers of the type, who specifies and best understands the runtime behaviour of that type, to also specify what that type will look like when it is display in debugger.
How to use DebuggerDisplay attribute:
using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var employee =new Employee(); } } public class Employee { [DebuggerDisplay("Id={EmployeeId}")] public int EmployeeId = 1; [DebuggerDisplay("Name={Name}")] public string Name = "Jalpesh Vadgama"; } }
Here you can see I have created a class called “Employee” with two variable with DebuggerDisplay Attribute and from main method I have created a object of employee class. Now let’s create debug the code via pressing F5 and see how its works.
As you can see its display data as defined in argument. So now you can format your debugger visualizer screen as per your requirement. That’s it. Hope you like it. Stay tuned for more.
Article title typo (needs correction for SEO benefit):
ReplyDeleteIS Enhaced debugging SHOULD BE Enhanced debugging
Thanks for pointing I have already corrected it. Thanks again for pointing.
DeleteThanks for sharing this wonderful post, It is awesome.
ReplyDeleteYou're welcome
Delete