Thursday, March 20, 2014

Enhanced debugging with DebuggerDisplay in C#

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.

How to use DebuggerDisplay attribute:


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.
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.

DisplayDeubggerAttribute

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.
Share:

4 comments:

  1. Article title typo (needs correction for SEO benefit):

    IS Enhaced debugging SHOULD BE Enhanced debugging

    ReplyDelete
    Replies
    1. Thanks for pointing I have already corrected it. Thanks again for pointing.

      Delete
  2. Thanks for sharing this wonderful post, It is awesome.

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews