While working on complex classes/objects, it really takes two more clicks to either add that object to the Watch window or expand the object in visualizer and navigate to specific property.
To avoid these extra steps and to make your debugging bit faster, you can decorate class with specific property using DebuggerDisplay attribute. Applying this attribute makes sure that when you add object to Watch window, it displays the value of above property in Value column.
To avoid these extra steps and to make your debugging bit faster, you can decorate class with specific property using DebuggerDisplay attribute. Applying this attribute makes sure that when you add object to Watch window, it displays the value of above property in Value column.
For
example, I have below Model class for which I am creating an object
and list of objects with some dummy values. Also, I have decorated
this class with DebuggerDisplay
attribute that has definition as to display name of the person, ID of
the person and length of a name.
///
<summary>
///
Debugger Display Attribute
–
Model class
///
</summary>
[DebuggerDisplay("Emp
ID = {name} - {ID} - {name.Length}")]
public
class
TestDebuggerDisplay
{
public
string
name
{
get;
set;
}
public
int
ID
{
get;
set;
}
}
Main
Class:
class
Program
{
static
void
Main(string[]
args)
{
TestDebuggerDisplay
obj = new
TestDebuggerDisplay()
{ ID=1, name="name1"
};
List<TestDebuggerDisplay>
lstObjs = new
List<TestDebuggerDisplay>();
for
(int
i = 0; i < 5; i++)
{
lstObjs.Add(new
TestDebuggerDisplay()
{
ID
= 10 * (i + 1),
name
= $"name{10
* (i + 1)}"
}
);
);
}
Console.WriteLine();
}
}
}
Output:
While
you are debugging, you would see that, when the object is added to
watch window you can see the value for that object as below:
No comments:
Post a Comment