Skip to content

Commit fb7d18a

Browse files
authored
Delete unnecessary spaces
1 parent 29cc80d commit fb7d18a

File tree

1 file changed

+158
-158
lines changed

1 file changed

+158
-158
lines changed

docs/debugger/using-the-debuggerdisplay-attribute.md

Lines changed: 158 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -2,177 +2,177 @@
22
title: "Using the DebuggerDisplay Attribute | Microsoft Docs"
33
ms.date: "01/09/2019"
44
ms.topic: "conceptual"
5-
helpviewer_keywords:
5+
helpviewer_keywords:
66
- "attributes, debugger"
77
- "DebuggerDisplay attribute"
88
- "DebuggerDisplayAttribute class"
99
ms.assetid: f4eb7c76-af4e-493b-9ab6-9cb05949d9b3
1010
author: "mikejo5000"
1111
ms.author: "mikejo"
1212
manager: jillfra
13-
ms.workload:
13+
ms.workload:
1414
- "multiple"
1515
---
1616
# Using the DebuggerDisplay Attribute (C#, Visual Basic, C++/CLI)
17-
The <xref:System.Diagnostics.DebuggerDisplayAttribute> controls how an object, property, or field is displayed in the debugger variable windows. This attribute can be applied to types, delegates, properties, fields, and assemblies.
18-
19-
The `DebuggerDisplay` attribute has a single argument, which is a string to be displayed in the value column for instances of the type. This string can contain braces (`{` and `}`). Text within a pair of braces is evaluated as a field, property or method.
20-
21-
If a class has an overridden `ToString()` method, the debugger uses the overridden method instead of the default `{<typeName>}`. Thus, if you have overridden the `ToString()` method, the debugger uses the overridden method instead of the default`{<typeName>}`, and you do not have to use `DebuggerDisplay`. If you use both, the `DebuggerDisplay` attribute takes precedence over the overridden `ToString()` method.
22-
23-
Whether the debugger evaluates this implicit `ToString()` call depends on a user setting in the **Tools / Options / Debugging** dialog box . Visual Basic does not implement this implicit `ToString()` evaluation.
24-
17+
The <xref:System.Diagnostics.DebuggerDisplayAttribute> controls how an object, property, or field is displayed in the debugger variable windows. This attribute can be applied to types, delegates, properties, fields, and assemblies.
18+
19+
The `DebuggerDisplay` attribute has a single argument, which is a string to be displayed in the value column for instances of the type. This string can contain braces (`{` and `}`). Text within a pair of braces is evaluated as a field, property or method.
20+
21+
If a class has an overridden `ToString()` method, the debugger uses the overridden method instead of the default `{<typeName>}`. Thus, if you have overridden the `ToString()` method, the debugger uses the overridden method instead of the default`{<typeName>}`, and you do not have to use `DebuggerDisplay`. If you use both, the `DebuggerDisplay` attribute takes precedence over the overridden `ToString()` method.
22+
23+
Whether the debugger evaluates this implicit `ToString()` call depends on a user setting in the **Tools / Options / Debugging** dialog box . Visual Basic does not implement this implicit `ToString()` evaluation.
24+
2525
> [!IMPORTANT]
26-
> If the **Show raw structure of objects in variables windows** check box is selected in the **Tools /Options / Debugging** dialog box, then the `DebuggerDisplay` attribute is ignored.
26+
> If the **Show raw structure of objects in variables windows** check box is selected in the **Tools /Options / Debugging** dialog box, then the `DebuggerDisplay` attribute is ignored.
2727
2828
> [!NOTE]
2929
> For native code, this attribute is supported only in C++/CLI code.
30-
31-
The following table shows some possible uses of the `DebuggerDisplay` attribute and example outputs.
32-
33-
|Attribute|Output appearing in the Value column|
34-
|---------------| - |
35-
|`[DebuggerDisplay("x = {x} y = {y}")]`<br /><br /> Used on a type with fields `x` and `y`.|`x = 5 y = 18`|
36-
|`[DebuggerDisplay("String value is {getString()}")]`Parameter syntax can vary between languages. Therefore, use it with care.|`String value is [5, 6, 6]`|
37-
38-
`DebuggerDisplay` can also accept named parameters.
39-
40-
|Parameters|Purpose|
41-
|----------------|-------------|
42-
|`Name`, `Type`|These parameters affect the **Name** and **Type** columns of the variable windows. (They can be set to strings using the same syntax as the constructor.)Overusing these parameters, or using them incorrectly, can cause confusing output.|
43-
|`Target`, `TargetTypeName`|Specifies the target type when the attribute is used at the assembly level.|
44-
45-
The autoexp.cs file uses the DebuggerDisplay attribute at the assembly level. The autoexp.cs file determines the default expansions that Visual Studio uses for .NET objects. You can examine the autoexp.cs file for examples of how to use the DebuggerDisplay attribute, or you can modify and compile the autoexp.cs file to change the default expansions. Be sure to back up the autoexp.cs file before you modify it.
46-
47-
To build autoexp.cs, open up a Developer Command Prompt for VS2015, and run the following commands
48-
30+
31+
The following table shows some possible uses of the `DebuggerDisplay` attribute and example outputs.
32+
33+
|Attribute|Output appearing in the Value column|
34+
|---------------| - |
35+
|`[DebuggerDisplay("x = {x} y = {y}")]`<br /><br /> Used on a type with fields `x` and `y`.|`x = 5 y = 18`|
36+
|`[DebuggerDisplay("String value is {getString()}")]`Parameter syntax can vary between languages. Therefore, use it with care.|`String value is [5, 6, 6]`|
37+
38+
`DebuggerDisplay` can also accept named parameters.
39+
40+
|Parameters|Purpose|
41+
|----------------|-------------|
42+
|`Name`, `Type`|These parameters affect the **Name** and **Type** columns of the variable windows. (They can be set to strings using the same syntax as the constructor.)Overusing these parameters, or using them incorrectly, can cause confusing output.|
43+
|`Target`, `TargetTypeName`|Specifies the target type when the attribute is used at the assembly level.|
44+
45+
The autoexp.cs file uses the DebuggerDisplay attribute at the assembly level. The autoexp.cs file determines the default expansions that Visual Studio uses for .NET objects. You can examine the autoexp.cs file for examples of how to use the DebuggerDisplay attribute, or you can modify and compile the autoexp.cs file to change the default expansions. Be sure to back up the autoexp.cs file before you modify it.
46+
47+
To build autoexp.cs, open up a Developer Command Prompt for VS2015, and run the following commands
48+
4949
```cmd
50-
cd <directory containing autoexp.cs>
51-
csc /t:library autoexp.cs
52-
```
53-
54-
The changes to autoexp.dll will be picked up in the next debug session.
55-
56-
## Using Expressions in DebuggerDisplay
57-
Although you can use a general expression between the braces in a DebuggerDisplay attribute, this practice is not recommended.
58-
59-
A general expression in DebuggerDisplay has implicit access to the `this` pointer for the current instance of the target type only. The expression has no access to aliases, locals, or pointers. If the expression references properties, attributes on those properties are not processed. For example, the C# code `[DebuggerDisplay("Object {count - 2}")]` would display `Object 6` if the field `count` was 8.
60-
61-
Using expressions in DebuggerDisplay can lead to the following issues:
62-
63-
- Evaluating expressions is the most expensive operation in the debugger and the expression is evaluated each time it is displayed. This can cause performance issues in stepping through code. For example, a complex expression that is used to display the values in a collection or list can be very slow when the number of elements is large.
64-
65-
- Expressions are evaluated by the expression evaluator of the language of the current stack frame and not by the evaluator of the language in which the expression was written. This can cause unpredictable results when the languages are different.
66-
67-
- Evaluating an expression can change the state of the application. For example, an expression that sets the value of a property mutates the property value in the executing code.
68-
69-
One way to reduce the possible problems of expression evaluation is by creating a private property that performs the operation and returns a string. The DebuggerDisplay attribute can then display the value of that private property. The following example implements this pattern:
70-
71-
```csharp
72-
[DebuggerDisplay("{DebuggerDisplay,nq}")]
73-
public sealed class MyClass
74-
{   
75-
public int count { get; set; }   
76-
public bool flag { get; set; }   
77-
private string DebuggerDisplay
78-
{       
79-
get
80-
{
81-
return string.Format("Object {0}", count - 2);
82-
}   
83-
}
84-
}
85-
```
86-
The ",nq" suffix tells the expression evaluator to remove the quotes when displaying the final value (nq = no quotes).
87-
88-
## Example
89-
The following code example shows how to use `DebuggerDisplay`, together with `DebuggerBrowseable` and `DebuggerTypeProxy`. When viewed in a debugger variables window, such as the **Watch** window, it produces an expansion that looks like this:
90-
91-
|**Name**|**Value**|**Type**|
92-
|--------------|---------------|--------------|
93-
|Key|"three"|object {string}|
94-
|Value|3|object {int}|
95-
96-
```csharp
97-
[DebuggerDisplay("{value}", Name = "{key}")]
98-
internal class KeyValuePairs
99-
{
100-
private IDictionary dictionary;
101-
private object key;
102-
private object value;
103-
public KeyValuePairs(IDictionary dictionary, object key, object value)
104-
{
105-
this.value = value;
106-
this.key = key;
107-
this.dictionary = dictionary;
108-
}
109-
110-
public object Key
111-
{
112-
get { return key; }
113-
set
114-
{
115-
object tempValue = dictionary[key];
116-
dictionary.Remove(key);
117-
key = value;
118-
dictionary.Add(key, tempValue);
119-
}
120-
}
121-
122-
public object Value
123-
{
124-
get { return this.value; }
125-
set
126-
{
127-
this.value = value;
128-
dictionary[key] = this.value;
129-
}
130-
}
131-
}
132-
133-
[DebuggerDisplay("{DebuggerDisplay,nq}")]
134-
[DebuggerTypeProxy(typeof(HashtableDebugView))]
135-
class MyHashtable
136-
{
137-
public Hashtable hashtable;
138-
139-
public MyHashtable()
140-
{
141-
hashtable = new Hashtable();
50+
cd <directory containing autoexp.cs>
51+
csc /t:library autoexp.cs
52+
```
53+
54+
The changes to autoexp.dll will be picked up in the next debug session.
55+
56+
## Using Expressions in DebuggerDisplay
57+
Although you can use a general expression between the braces in a DebuggerDisplay attribute, this practice is not recommended.
58+
59+
A general expression in DebuggerDisplay has implicit access to the `this` pointer for the current instance of the target type only. The expression has no access to aliases, locals, or pointers. If the expression references properties, attributes on those properties are not processed. For example, the C# code `[DebuggerDisplay("Object {count - 2}")]` would display `Object 6` if the field `count` was 8.
60+
61+
Using expressions in DebuggerDisplay can lead to the following issues:
62+
63+
- Evaluating expressions is the most expensive operation in the debugger and the expression is evaluated each time it is displayed. This can cause performance issues in stepping through code. For example, a complex expression that is used to display the values in a collection or list can be very slow when the number of elements is large.
64+
65+
- Expressions are evaluated by the expression evaluator of the language of the current stack frame and not by the evaluator of the language in which the expression was written. This can cause unpredictable results when the languages are different.
66+
67+
- Evaluating an expression can change the state of the application. For example, an expression that sets the value of a property mutates the property value in the executing code.
68+
69+
One way to reduce the possible problems of expression evaluation is by creating a private property that performs the operation and returns a string. The DebuggerDisplay attribute can then display the value of that private property. The following example implements this pattern:
70+
71+
```csharp
72+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
73+
public sealed class MyClass
74+
{
75+
public int count { get; set; }
76+
public bool flag { get; set; }
77+
private string DebuggerDisplay
78+
{
79+
get
80+
{
81+
return string.Format("Object {0}", count - 2);
82+
}
83+
}
84+
}
85+
```
86+
The ",nq" suffix tells the expression evaluator to remove the quotes when displaying the final value (nq = no quotes).
87+
88+
## Example
89+
The following code example shows how to use `DebuggerDisplay`, together with `DebuggerBrowseable` and `DebuggerTypeProxy`. When viewed in a debugger variables window, such as the **Watch** window, it produces an expansion that looks like this:
90+
91+
|**Name**|**Value**|**Type**|
92+
|--------------|---------------|--------------|
93+
|Key|"three"|object {string}|
94+
|Value|3|object {int}|
95+
96+
```csharp
97+
[DebuggerDisplay("{value}", Name = "{key}")]
98+
internal class KeyValuePairs
99+
{
100+
private IDictionary dictionary;
101+
private object key;
102+
private object value;
103+
public KeyValuePairs(IDictionary dictionary, object key, object value)
104+
{
105+
this.value = value;
106+
this.key = key;
107+
this.dictionary = dictionary;
108+
}
109+
110+
public object Key
111+
{
112+
get { return key; }
113+
set
114+
{
115+
object tempValue = dictionary[key];
116+
dictionary.Remove(key);
117+
key = value;
118+
dictionary.Add(key, tempValue);
119+
}
120+
}
121+
122+
public object Value
123+
{
124+
get { return this.value; }
125+
set
126+
{
127+
this.value = value;
128+
dictionary[key] = this.value;
129+
}
142130
}
143-
144-
private string DebuggerDisplay { get { return "Count = " + hashtable.Count); } }
145-
146-
private class HashtableDebugView
147-
{
148-
private MyHashtable myhashtable;
149-
public HashtableDebugView(MyHashtable myhashtable)
150-
{
151-
this.myhashtable = myhashtable;
152-
}
153-
154-
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
155-
public KeyValuePairs[] Keys
156-
{
157-
get
158-
{
159-
KeyValuePairs[] keys = new KeyValuePairs[myhashtable.hashtable.Count];
160-
161-
int i = 0;
162-
foreach (object key in myhashtable.hashtable.Keys)
163-
{
164-
keys[i] = new KeyValuePairs(myhashtable.hashtable, key, myhashtable.hashtable[key]);
165-
i++;
166-
}
167-
return keys;
168-
}
169-
}
170-
}
171-
}
172-
```
173-
174-
## See Also
175-
[Using DebuggerTypeProxy Attribute](../debugger/using-debuggertypeproxy-attribute.md)
176-
[Create custom views of managed objects](../debugger/create-custom-views-of-dot-managed-objects.md)
177-
[Format specifiers in C#](../debugger/format-specifiers-in-csharp.md)
178-
[Enhancing Debugging with the Debugger Display Attributes](/dotnet/framework/debug-trace-profile/enhancing-debugging-with-the-debugger-display-attributes)
131+
}
132+
133+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
134+
[DebuggerTypeProxy(typeof(HashtableDebugView))]
135+
class MyHashtable
136+
{
137+
public Hashtable hashtable;
138+
139+
public MyHashtable()
140+
{
141+
hashtable = new Hashtable();
142+
}
143+
144+
private string DebuggerDisplay { get { return "Count = " + hashtable.Count); } }
145+
146+
private class HashtableDebugView
147+
{
148+
private MyHashtable myhashtable;
149+
public HashtableDebugView(MyHashtable myhashtable)
150+
{
151+
this.myhashtable = myhashtable;
152+
}
153+
154+
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
155+
public KeyValuePairs[] Keys
156+
{
157+
get
158+
{
159+
KeyValuePairs[] keys = new KeyValuePairs[myhashtable.hashtable.Count];
160+
161+
int i = 0;
162+
foreach (object key in myhashtable.hashtable.Keys)
163+
{
164+
keys[i] = new KeyValuePairs(myhashtable.hashtable, key, myhashtable.hashtable[key]);
165+
i++;
166+
}
167+
return keys;
168+
}
169+
}
170+
}
171+
}
172+
```
173+
174+
## See Also
175+
[Using DebuggerTypeProxy Attribute](../debugger/using-debuggertypeproxy-attribute.md)
176+
[Create custom views of managed objects](../debugger/create-custom-views-of-dot-managed-objects.md)
177+
[Format specifiers in C#](../debugger/format-specifiers-in-csharp.md)
178+
[Enhancing Debugging with the Debugger Display Attributes](/dotnet/framework/debug-trace-profile/enhancing-debugging-with-the-debugger-display-attributes)

0 commit comments

Comments
 (0)