Share via


Debugger Views Application Sample

This sample demonstrates a feature introduced with Microsoft Visual Studio 2005, the capability to change the way you view a class in the debug windows. By applying new attributes such as the DebuggerDisplayAttribute to a class and its members, you can control how the value, type, and name columns are displayed for that type in the Locals debug window, and whether a value is shown, hidden, or expanded when viewed. This sample is currently available only in Microsoft Visual C# 2005.

For information about using the samples, see the following topics:

This sample includes source files for three classes that contain the debugger attributes and source files that do not, as shown in the following table.

Files with debugger attributes

Files without debugger attributes

DebugAddress.cs

NonDebugAddress.cs

DebugCustomer.cs

NonDebugCustomer.cs

DebugCustomerName.cs

NonDebugCustomerName.cs

The DebuggerViewsExample.cs file contains the entry point of the console application and is used by both sets of files. Uncomment the code in the debug versions (DebugAddress.cs, DebugCustomer.cs, and DebugCustomerName.cs) to run the sample.

To build the sample using the Command Prompt

  1. Open a Command Prompt window and navigate to the \CS subdirectory under the DebuggerViewsCS directory. For information about required settings and the SDK Command Prompt, see How to: Set Sample Settings.

  2. Type msbuild DebuggerViewsCS.sln at the command line.

    Note

    This sample demonstrates features of Visual Studio. You must start and run it within the Visual Studio debugging environment to view its output.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to the \CS subdirectory under the DebuggerViewsCS directory.

  2. Double-click DebuggerViewsCS.sln to open the file in Visual Studio 2005.

  3. On the Build menu, click Build Solution.

To run the sample

  1. On the View menu of Visual Studio, click Solution Explorer.

  2. In the Solution Explorer window, double-click DebuggerViewsExample.cs to open the file in Visual Studio 2005.

  3. On the Project menu, click Show all files.

  4. Insert a breakpoint (press F9) on the line where the GetCustomers method instantiates a variable of type Customer, as shown here:

    Customer c = new Customer(cn, Address.GetAddressById(customerAddresses, cn.Id));

  5. Press F5 to start and run the sample application in the Visual Studio 2005 debugging environment.

  6. When the application breaks at the breakpoint, press F10 to step to the next line of code.

  7. Observe current values in the Locals window by expanding the hierarchy of the application's properties, collections, and other objects. To display the Locals window, click Windows from the Debug menu and then click Locals.

  8. On the Debug menu, click Stop Debugging, press Shift+F5, or click the icon in the Debugging toolbar, to stop code execution.

  9. Uncomment the code in the three files whose names start with Debug. Then comment out the code in the three files whose names start with NonDebug, or select those three files in the Solution Explorer window, right-click, and then click Exclude From Project.

  10. On the Debug menu, choose Start with the same breakpoint set. When the application breaks, observe the details in the Locals window values column, which now has additional information provided by debugging attributes.

Remarks

To provide an example of using a debugging attribute, the DebugCustomer.cs file contains the following DebuggerDisplay attribute for the CustomerName class:

[DebuggerDisplay("{(FirstName == null) ? \"\":FirstName} {LastName} ==> (Customer ID = {Id})")]

DebuggerTypeProxy(typeof(CustomerNameDebugView))]

public class CustomerName

When debugging, this attribute displays the following value in the Locals window. The name cn is an instance of a CustomerName object.

Name

Value

Type

cn

"Sally" "HighBridge" ==> (Customer ID = 12345)

Microsoft.Samples.CustomerName

Without the attribute, the Locals window displays the following:

Name

Value

Type

cn

{Microsoft.Samples.CustomerName}

Microsoft.Samples.CustomerName

See Also

Other Resources

Debugging in Visual Studio