Customize object displays in the Visual Studio debugger YOUR way

Leslie Richardson on not so commonly used attribute DebuggerDisplay:

By writing DebuggerDisplay syntax at the top of a class, you can choose what strings and properties you want at the top of each object node in debugger windows. Besides displaying strings in debugger windows, adding curly brackets ({}) to the DebuggerDisplay attribute allows Visual Studio to display the value of a property or method that you specify.

I attempt to create a small console application below to follow the usage of DebuggerDisplay.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace ConsoleApp
{
  [DebuggerDisplay("{LastName}, {FirstName}")]
  public class Worker
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Address { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string Zip { get; set; }
      public string Phone { get; set; }
  }

  class Program
  {
      static void Main(string[] args)
      {
          List<Worker> workers = new List<Worker>();

          List<Worker> workers = new List<Worker>();
          workers.Add(
              new Worker {
                  FirstName = "Michael",
                  LastName = "Springer",
                  City = "Atlanta",
                  Phone = "001-232-1233"
              });
          workers.Add(
              new Worker {
                  FirstName = "Lloyd",
                  LastName = "Bever",
                  City = "Fort Myers",
                  Phone = ""
              });
          workers.Add(
              new Worker {
                  FirstName = "Vincent",
                  LastName = "Covey",
                  City = "Chester Heights",
                  Phone = "888-232-1233"
              });

          Console.WriteLine(workers.Count);
          Console.ReadKey();
      }
  }
}
QuickWatch - Without DebuggerDisplay
QuickWatch - With DebuggerDisplay

You can also use nq (no quote) specifier if you don’t prefer to see quotes in debug values.

1
2
3
4
5
6
  [DebuggerDisplay("{LastName, nq}, {FirstName, nq}")]
  public class Worker
  {
    //...
  }
}
QuickWatch - DebuggerDisplay with No Quote Specifier

You can further extend DebuggerDisplay if you want to include an expression in debug values. Just add a private method or property in the target class, and provide that method or property to DebuggerDisplay.

1
2
3
4
5
6
7
8
9
[DebuggerDisplay("{DebuggerString(), nq}")]
public class Worker
{
  private string DebuggerString()
  {
      return string.Format($"{FirstName}, {LastName}. Has Phone: {!Phone.Equals(string.Empty)}");
  }
  //...
}
QuickWatch - DebuggerDisplay using Expression

Text Editor - Find Text Using Regular Expressions

It’s in our muscle memory. Press Ctrl+F in your favorite text editor to find something, provide find criteria, and hit the Find button. As always, we usually stick with the simple Find functionality of the text editor as it serves almost all of our basic search needs. But beneath Find dialog box, there is a powerful Find option, Use Regular Expressions which most seldom use.

Visual Studio 2019 - Find in Files

Able to recall a correct regular expression when we need it most is rare. Which often leads to the hassle of googling regular expression and trying few failed attempts, and going back to simple Find. This is where following regular expressions cheatsheet comes handy for me.

Regular Expressions:

  • Have foo anywhere and bar later in the same line

    .*foo.*bar

  • Have foo at the beginning of the line

    ^\s{2,}foo

  • Have foo at the beginning of the line and bar later in the same line

    ^\s{2,}foo.*bar

  • Have foo in the commentted line

    .*//.*foo

  • Have foo inside double quotes

    ^.*".*foo.*"

  • Have foo at the beginning or end of the word

    ^.*".*\bfoo.*"

  • Have foo at the end of the line

    foo\r?\n

  • Have foo); at the end of the line

    foo\);\r?\n

  • Have foo but not bar in the same line

    ^.*foo.*(?!bar)

  • Have foo and { in the next line

    .*foo.*\r?\n.*{