Menu
Fragrance
|
There are many hidden tricks which are present in Visual Studio 2017 and can be really very useful. And one of them which I will be discussing today is "DebuggerDisplay".
Debugger Display helps to display runtime object data in simple and meaningful way. Of course meaningful data differs from developer to developer and also depends on what we are debugging. At least Debugger display gives control what to display and with the help of attributed debugging. Attributed debugging gives type the ability to specify what it looks like when being debugged. The simplest form of this is the DebuggerDisplayAttribute. Below is the sample code.
After writing the above code, set a breakpoint at the end of the Main class and if you watch "Employees" in Watch, you will see something as below:
Now if you want to examine any of the employee object you first need to know which object inside the Employees collection. You can even override ToString and display the complete name but that will be functional evaluation and will be very slower and also not necessary the ToString implementation and the Debug view is same. So in such case "DebuggerDisplay" comes very handy and you can use it as shown below
After attaching the above attribute in the Employee class, you can see that now Debug display more sensible data and it's very easy to locate the object in the collection.
If you want you can even include the format specifiers in the expression.
Comments
|
Categories
All
Archives
January 2019
|