How to make better object representation in debugger watch?

For example you have following class:

public class Person

{

   public Person(string firstName,string lastName)

   {

       _firstName = firstName;

       _lastName = lastName;

   } 

   private string _firstName;

   private string _lastName;

}

Debugger watch window is going to show you: YouNamespace.Person.

However if you declare your class this way:

[DebuggerDisplay("{ToString()}")]

publicclassPerson

{

        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 result of:

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

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

Sunday, June 15, 2008 | Add Comment

New Comment

Your Name:
Email (for internal use only):
Subject:
Comment:
 
Code above: