KARPACH

WEB DEVELOPER BLOG

How to make better object representation in debugger watch?

For example, you have the following class:

public class Person
{
    public Person(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }
    private string _firstName;
    private string _lastName;
}

The debugger watch window is going to show you: YouNamespace.Person.

However, if you declare your class this way:

[DebuggerDisplay("{ToString()}")]
public class Person
{
    public Person(string firstName,string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }

    public override string ToString()
    {
        return string.Concat(_firstName, " ", _lastName);
    }
    private string _firstName;
    private string _lastName;
}

And check the result of:

Person p = new Person("Viktar", "Karpach");

You are going to see in the debugger watch window that p has the value Viktar Karpach.

Posted on June 15, 2008 by

Comments

Posted on 7/28/2009 03:49:09 AM by kalavadiya Narendra

good example