Udostępnij za pośrednictwem


Using DebuggerDisplay Attribute

This topic applies to:

Edition

Visual Basic

C#

C++

Web Developer

Express

Topic applies Topic applies Topic applies Topic applies

Standard

Topic applies

Topic applies

Topic applies

Topic applies

Pro and Team

Topic applies

Topic applies

Topic applies

Topic applies

Table legend:

Topic applies

Applies

Topic does not apply

Does not apply

Topic applies but command hidden by default

Command or commands hidden by default.

The DebuggerDisplay attribute (System.Diagnostics.DebuggerDisplayAttribute) controls how a class or field is displayed in the debugger variable windows. This attribute can be applied to:

  • Classes

  • Structs

  • Delegates

  • Enums

  • Fields

  • Properties

  • Assemblies

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 will be evaluated as a field, property or method.

In C# code, you can use a general expression between the braces. The expression 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.

If a C# object has an overridden ToString(), the debugger will call the override and show its result instead of the standard {<typeName>}. Thus, if you have overridden ToString(), you do not have to use DebuggerDisplay. If you use both, the DebuggerDisplay attribute takes precedence over the ToString() override.

Whether the debugger evaluates this implicit ToString() call depends on a user setting in the Options dialog box (Debugging category, General page). Visual Basic does not implement this implicit ToString() evaluation.

The following table shows some possible uses of the DebuggerDisplay attribute and example outputs.

Attribute

Output appearing in the Value column)

[DebuggerDisplay("x = {x} y = {y}")]

Used on a type with fields x and y.

x = 5 y = 18

[DebuggerDisplay("String value is {getString()}")]

Parameter syntax can vary between languages. Therefore, use it with care.

String value is [5, 6, 6]

[DebuggerDisplay("Object {count - 2}: {(flag) ? \"yes\" : \"no\"}")]

Expression syntax varies between languages. Therefore, use it with care.

Object 6: yes

[DebuggerDisplay("Last = {_lastName,nq} {_var == null ? \"\" : \"First = \" + _firstName,nq}")]

,nq strips away the quotation marks.

If last name is present

Last = lastname First = firstname

otherwise:

Last = lastname

DebuggerDisplay can also accept named parameters.

Parameters

Purpose

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.

Target, TargetTypeName

Specifies the target type when the attribute is used at the assembly level.

Note

The autoexp.cs file uses the DebuggerDisplay attribute at the assembly level. The autoexp.cs file determines the default expansions Visual Studio uses for C# variables. 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. You must reference Microsoft.VisualStudio.DebuggerVisualizers.dll in \Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies. You can find the autoexp.cs file in My Documents/Visual Studio 9.0/Visualizers.

Example

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:

Name

Value

Type

Key

"three"

object {string}

Value

3

object {int}

[DebuggerDisplay("{value}", Name = "{key}")]
internal class KeyValuePairs
{
    private IDictionary dictionary;
    private object key;
    private object value;

    public KeyValuePairs(IDictionary dictionary, object key, object value)
    {
        this.value = value;
        this.key = key;
        this.dictionary = dictionary;
    }

    public object Key
    {
        get { return key; }
        set
        {
            object tempValue = dictionary[key];
            dictionary.Remove(key);
            key = value;
            dictionary.Add(key, tempValue);
        }
    }

    public object Value
    {
        get { return this.value; }
        set
        {
            this.value = value;
            dictionary[key] = this.value;
        }
    }
}

[DebuggerDisplay("Count = {hashtable.Count}")]
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable
{
    public Hashtable hashtable;

    public MyHashtable()
    {
        hashtable = new Hashtable();  
    }

    private class HashtableDebugView
    {
        private MyHashtable myhashtable;
        public HashtableDebugView(MyHashtable myhashtable)
        {
            this.myhashtable = myhashtable;
        }

        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        public KeyValuePairs[] Keys
        {
            get
            {
                KeyValuePairs[] keys = new KeyValuePairs[myhashtable.hashtable.Count];

                int i = 0;
                foreach (object key in myhashtable.hashtable.Keys)
                {
                    keys[i] = new KeyValuePairs(myhashtable.hashtable, key, myhashtable.hashtable[key]);
                    i++;
                }
                return keys;
            }
        }
    }
}

See Also

Concepts

Displaying Custom Data Types

Enhancing Debugging with the Debugger Display Attributes

Reference

Using DebuggerTypeProxy Attribute