
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating Custom Debugging Displays for Your Classes
|
479
Now when instances of the Citizen class are instantiated, the debugger will show the
information the way the
DebuggerDisplayAttribute on the class directs it to. To see
this, instantiate two
Citizens, Mrs. Alice G. Jones and Mr. Robert Frederick Jones
like this:
Citizen mrsJones = new Citizen("Mrs.","Alice","G.","Jones");
Citizen mrJones = new Citizen("Mr.", "Robert", "Frederick", "Jones");
When this code is run under the debugger, the custom display is used, as shown in
Figure 8-1.
Discussion
It is nice to be able to see the pertinent information for classes you write quickly. But
the more powerful part of this feature is the ability for your team members to quickly
understand what this class instance holds. The
this pointer is accessible from the
DebuggerDisplayAttribute declaration, but any properties accessed using the this
pointer will not evaluate the property attributes before processing. Essentially, if you
access a property on the current object instance as part of constructing the display
string, if that property has attributes, they will not be processed, and therefore you
may not get the value you thought you would. If you have custom
ToString( ) over-
rides in place already, the debugger will use these as the
DebuggerDisplayAttribute ...