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()}")]

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

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

 

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


Posted on Sunday, June 15, 2008 by | Comments (1) | Add Comment

Comments

Gravatar

Re:How to make better object representation in debugger watch?

good example

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

New Comment

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

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5