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
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.
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
Open Windows Explorer and navigate to the \CS subdirectory under the DebuggerViewsCS directory.
Double-click DebuggerViewsCS.sln to open the file in Visual Studio 2005.
On the Build menu, click Build Solution.
To run the sample
On the View menu of Visual Studio, click Solution Explorer.
In the Solution Explorer window, double-click DebuggerViewsExample.cs to open the file in Visual Studio 2005.
On the Project menu, click Show all files.
Insert a breakpoint (press F9) on the line where the
GetCustomers
method instantiates a variable of typeCustomer
, as shown here:Customer c = new Customer(cn, Address.GetAddressById(customerAddresses, cn.Id));
Press F5 to start and run the sample application in the Visual Studio 2005 debugging environment.
When the application breaks at the breakpoint, press F10 to step to the next line of code.
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.
On the Debug menu, click Stop Debugging, press Shift+F5, or click the icon in the Debugging toolbar, to stop code execution.
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.
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})")]
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 |