Creating Custom Debugging Displays For Your Classes


Points to remember while setting debugger display:

  1. Debugger identifies only the Property name placed inside the curly brace {Property}.
  2. Example: {Name}={PropertyName}.
  3. Nested Class Example : {ClassName.PropertyName}={Product.Name}.

Step 1: Namespaces used:

using System;
using System.Diagnostics;

Step 2: Usage:

protected void Page_Load(object sender, EventArgs e)
{
    Book book = new Book();
    book.Name = "Practical Demo";
    book.Title = "CSharp";
    book.Register = DateTime.Today;

    Product product=new Product();
    product.Name = "Programming";
    book.Product = product;

}

Step 3: Class for demo:

[DebuggerDisplay("BookTitle{Title}-- {Name} , {Register}--{Product.Name}")]
    public class Book
    {
        public string Title;
        public DateTime Register;
        public string Name;
        public Product Product;
    }

Step 4: Nested class for demo:

[DebuggerDisplay("ProductName{Name}")]
    public class Product
    {
        public string Name;
    }

Step 5: Format of Class Debugger Display:

[DebuggerDisplay("ProductName{Name}")]

Step 6: Format of Nested Class Debugger Display:

[DebuggerDisplay("BookTitle{Title}-- {Name} , {Register}--{Product.Name}")]

Step 7: Output:



Debug.gif

Code Snippet:

using System;
using System.Diagnostics;

namespace SampleApplication.New
{
    public partial class DebuggerDisplatClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Book book = new Book();
            book.Name = "Practical Demo";
            book.Title = "CSharp";
            book.Register = DateTime.Today;

            Product product=new Product();
            product.Name = "Programming";
            book.Product = product;
        }
    }

    [DebuggerDisplay("BookTitle{Title}-- {Name} , {Register}--{Product.Name}")]
    public class Book
    {
        public string Title;
        public DateTime Register;
        public string Name;
        public Product Product;
    }

    [DebuggerDisplay("ProductName{Name}")]
    public class Product
    {
        public string Name;
    }
}


Thanks for reading this article.


Similar Articles